Skip to main content
added 134 characters in body
Source Link
David Spector
  • 1.6k
  • 16
  • 24

As of PHP 7.4.13 my experience is that all possible errors and exceptions in a program can be caught with only two callback functions:

set_error_handler("ErrorCB");
set_exception_handler("ExceptCB");

ErrorCB simply reports its arguments in any way desired and calls Exit().

ExceptCB calls "get" methods on its exception argument and does some logic to determine where the file, line, and function are (ask me if you would like details), and reports the information in any way desired and returns.

The only need for try/catch is if you need to suppress errors for particularcertain code, when @ failsor isset() isn't enough. Using try/catch for a "main function" without setting handlers fails, since it doesn't catch all errors.

If anyone finds code that generates an error that this approach doesn't catch, please let me know and I'll edit this answer. One error that this approach can't intercept is a single { character near the end of a PHP program; this generates a Parse error, which mightrequires that you run your main PHP program via an Include file that contains the error handling.

I haven't found any need for register_shutdown_function().

Note that all I care about is reporting errors and then quitting the program; I don't need to recover from errors--that would be a much more difficult question indeed.

As of PHP 7.4.13 my experience is that all possible errors and exceptions in a program can be caught with only two callback functions:

set_error_handler("ErrorCB");
set_exception_handler("ExceptCB");

ErrorCB simply reports its arguments in any way desired and calls Exit().

ExceptCB calls "get" methods on its exception argument and does some logic to determine where the file, line, and function are (ask me if you would like details), and reports the information in any way desired and returns.

The only need for try/catch is if you need to suppress errors for particular code, when @ fails. Using try/catch for a "main function" without setting handlers fails, since it doesn't catch all errors.

If anyone finds code that generates an error that this approach doesn't catch, please let me know and I'll edit this answer. One error that this approach can't intercept is a single { character near the end of a PHP program; this generates a Parse error, which might need register_shutdown_function().

Note that all I care about is reporting errors and then quitting the program; I don't need to recover from errors--that would be a much more difficult question indeed.

As of PHP 7.4.13 my experience is that all possible errors and exceptions in a program can be caught with only two callback functions:

set_error_handler("ErrorCB");
set_exception_handler("ExceptCB");

ErrorCB simply reports its arguments in any way desired and calls Exit().

ExceptCB calls "get" methods on its exception argument and does some logic to determine where the file, line, and function are (ask me if you would like details), and reports the information in any way desired and returns.

The only need for try/catch is if you need to suppress errors for certain code, when @ or isset() isn't enough. Using try/catch for a "main function" without setting handlers fails, since it doesn't catch all errors.

If anyone finds code that generates an error that this approach doesn't catch, please let me know and I'll edit this answer. One error that this approach can't intercept is a single { character near the end of a PHP program; this generates a Parse error, which requires that you run your main PHP program via an Include file that contains the error handling.

I haven't found any need for register_shutdown_function().

Note that all I care about is reporting errors and then quitting the program; I don't need to recover from errors--that would be a much more difficult question indeed.

Source Link
David Spector
  • 1.6k
  • 16
  • 24

As of PHP 7.4.13 my experience is that all possible errors and exceptions in a program can be caught with only two callback functions:

set_error_handler("ErrorCB");
set_exception_handler("ExceptCB");

ErrorCB simply reports its arguments in any way desired and calls Exit().

ExceptCB calls "get" methods on its exception argument and does some logic to determine where the file, line, and function are (ask me if you would like details), and reports the information in any way desired and returns.

The only need for try/catch is if you need to suppress errors for particular code, when @ fails. Using try/catch for a "main function" without setting handlers fails, since it doesn't catch all errors.

If anyone finds code that generates an error that this approach doesn't catch, please let me know and I'll edit this answer. One error that this approach can't intercept is a single { character near the end of a PHP program; this generates a Parse error, which might need register_shutdown_function().

Note that all I care about is reporting errors and then quitting the program; I don't need to recover from errors--that would be a much more difficult question indeed.