0

i've try this example :

<?php    
try {
    Not_Exist();
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

http://php.net/manual/en/language.exceptions.php But the error is not catched :

Fatal error: Call to undefined function Not_Exist() in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\mysite\public_html\exception.php on line 3

Why? I'm usually using Dotnet. Maybe i miss something.

George Brighton
  • 5,131
  • 9
  • 27
  • 36
Portekoi
  • 1,087
  • 2
  • 22
  • 44

3 Answers3

5

error is not an exception. PHP itself doesn't throw exceptions on syntax errors, such as missing functions. You'd need set_error_handler() to "catch" such things - and even then, a custom error handler can't handle parse errors such as this.

Marc B
  • 356,200
  • 43
  • 426
  • 500
2

There is no Exception being thrown in your code. You're simply hitting a fatal error. If you're trying to find a way around fatal errors, this post may be of help. How do I catch a PHP Fatal Error

If you're trying to catch Exceptions, something like this:

try {
   throw new Exception("Exception Message");
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
Community
  • 1
  • 1
2

Because that is a fatal error, not an exception. You're going to want to use PHP's function_exists() method and try something like:

try{
    function_exists(Not_Exist);
} catch (Exception $e) {
    // error here
}
// run Not_Exist here because we know its a valid function.
binaryatrocity
  • 956
  • 5
  • 10