moon 111 Posted April 20, 2008 Share Posted April 20, 2008 For some reason the following code is giving me a fatal error instead of an exception. <?php try { $instance = new NonexistentClass; } catch(Exception $error) { echo $error->getMessage(); } ?> Whats the problem? Thanks Link to comment https://forums.phpfreaks.com/topic/102019-exception-problems/ Share on other sites More sharing options...
thebadbad Posted April 20, 2008 Share Posted April 20, 2008 I'm totally new to exceptions in PHP, but aren't you supposed to throw exceptions yourself? E.g. with a function: <?php class someName { //defined class } function validate($class) { if (!class_exists($class)) { throw new Exception("Class \"$class\" doesn't exist."); } else { echo 'Class "', $class, '" exists.<br />'; } } try { validate('someName'); validate('NonexistentClass'); } catch(Exception $e) { echo 'Error: ', $e->getMessage(); } ?> Output: Class "someName" exists. Error: Class "NonexistentClass" doesn't exist. In case I just don't get it yet, disregard what I said Link to comment https://forums.phpfreaks.com/topic/102019-exception-problems/#findComment-522137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.