1

Possible Duplicate:
How do I catch a PHP Fatal Error

I want to create a custom error handler but PHP ignores it still uses the built in error handler. The example below gives a built in fatal error because test() is not defined:

 function critical_error($error_number,$error_string,$error_file,$error_line,$error_context) {
     echo "<strong>error:</strong> echo ".$error_string;
     exit();
 }

 set_error_handler("critical_error",8191);
 test();
 exit;

Any advice?

Community
  • 1
  • 1
mauzilla
  • 3,574
  • 10
  • 50
  • 86

1 Answers1

5

You are probably defining the error level wrong. Try using:

set_error_handler("critical_error", E_ALL);

Also notice that:

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

But without your complete script (including test()) it's hard to tell. I've tested it on my local server and everything works fine.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • Don't think he is, 8191 is everything from E_RECOVERABLE_ERROR and above. http://php.net/manual/en/errorfunc.constants.php – Jon Taylor Dec 19 '12 at 08:52
  • You're right about the notice part. _Fatal error: Call to undefined function_ is `E_ERROR` and user defined error handler won't be fired. – Salman A Dec 19 '12 at 08:59
  • 2
    Thank you for your advice, I think the issue was here that I did not add the E_ALL. test() is not defined, I wanted PHP to give me an fatal error to see if the custom error handler will catch it. – mauzilla Dec 19 '12 at 09:12