Home >Backend Development >PHP Tutorial >PHP exception handling, error throwing and callback functions and other object-oriented error handling methods_PHP tutorial
Exception handling is used to alter the normal flow of a script when a specified error (exception) condition occurs. This situation is called an exception.
PHP 5 adds an exception handling module similar to other languages. Exceptions generated in PHP code can be thrown by the throw statement and caught by the catch statement. Code that requires exception handling must be placed in a try code block to catch possible exceptions. Every try must have at least one corresponding catch. Use multiple catches to catch exceptions generated by different classes. When the try block no longer throws an exception or no catch is found that matches the thrown exception, the PHP code continues execution after jumping to the last catch. Of course, PHP allows exceptions to be thrown again within catch blocks.
When an exception is thrown, the subsequent code (Translator's Note: refers to the code block when the exception is thrown) will not continue to execute, and PHP will try to find the first one that can match The matching catch. If an exception is not caught and is not handled accordingly using set_exception_handler(), PHP will generate a serious error and output an Uncaught Exception... (uncaught exception) message.
When an exception is triggered, what typically happens:
• The current code state is saved
• Code execution is switched to a predefined exception handler function
•Depending on the situation, the processor may restart code execution from the saved code state, terminate script execution, or continue script execution from another location in the code
1. Error and exception level constant table
error: A runtime error that cannot be found during compilation. It is better to try to use echo to output an unassigned variable. Such problems often cause the program or logic to be unable to continue and need to be interrupted;
exception: program execution Unexpected situations occur during the process, which are often feasible logically, but do not meet the application scenarios. For example, receiving a user name with a length that is in the wrong predetermined format. Therefore, exceptions mainly rely on coders to make pre-judgements and then throw them. After catching the exception, change the program flow to handle these situations without interrupting the program.
PHP’s definition of exceptions and errors does not seem to be very obvious, especially in lower versions of PHP.
Error and logging values Constant Description Remarks
1 E_ERROR (integer)
Fatal runtime error. This type of error is generally an unrecoverable situation, such as a problem caused by memory allocation. The consequence is that the script terminates and does not continue to run.
2 E_WARNING (integer)
Runtime warning (non-fatal error). Only a prompt message is given, but the script does not terminate.
4 E_PARSE (integer)
Compile time syntax parsing error. Parsing errors are generated only by the parser.
8 E_NOTICE (integer)
Runtime notification. Indicates that the script encounters a situation that may appear as an error, but there may also be similar notifications in scripts that can run normally.
16 E_CORE_ERROR(integer)
A fatal error occurred during PHP initialization startup. This error is similar to E_ERROR, but is generated by the PHP engine core. since PHP 4
32 E_CORE_WARNING(integer)
Warning (non-fatal error) that occurred during PHP initialization startup. Similar to E_WARNING, but generated by the PHP engine core. since PHP 4
64 E_COMPILE_ERROR(integer)
Fatal compile-time error. Similar to E_ERROR, but generated by the Zend script engine. since PHP 4
128 E_COMPILE_WARNING(integer)
Compile time warning (non-fatal error). Similar to E_WARNING, but generated by the Zend scripting engine. since PHP 4
256 E_USER_ERROR(integer)
User-generated error message. Similar to E_ERROR, but is generated by the user using the PHP function trigger_error() in the code. since PHP 4
512 E_USER_WARNING(integer)
Warning message generated by the user. Similar to E_WARNING, but is generated by the user using the PHP function trigger_error() in the code. since PHP 4
1024 E_USER_NOTICE(integer)
Notification information generated by the user. Similar to E_NOTICE, but is generated by the user using the PHP function trigger_error() in the code. since PHP 4
2048 E_STRICT (integer)
Enable PHP's suggestions for code modifications to ensure the best interoperability and forward compatibility of the code. since PHP 5
4096 E_RECOVERABLE_ERROR(integer)
A fatal error that can be caught. It indicates that a potentially dangerous error has occurred, but has not caused the PHP engine to become unstable. If the error is not caught by a user-defined handler (see set_error_handler()), it will become an E_ERROR and the script will terminate. since PHP 5.2.0
8192 E_DEPRECATED(integer)
Runtime notification. When enabled, a warning will be given about code that may not work properly in future versions. since PHP 5.3.0
16384 E_USER_DEPRECATED(integer)
User generated warning message. Similar to E_DEPRECATED, but is generated by the user using the PHP function trigger_error() in the code. since PHP 5.3.0
30719 E_ALL (integer)
E_STRICT All error and warning messages.30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously
2. error_reporting() and try-catch, thrown
error_reporting() function can be obtained (not When passing parameters), set which exceptions the script handles (not all exceptions need to be handled, for example, E_CORE_WARNING, E_NOTICE, and E_DEPRECATED can be ignored). This setting will override the exception handling settings defined by the error_reporting option in php.ini.
For example:
error_reporting(E_ALL&~E_NOTICE); // Except for E_NOTICE, other exceptions will be triggered (the binary operation result of E_ALL&~E_NOTICE is: the value of the corresponding bit of E_NOTICE is set to 0) try-catch cannot be used in the class The automatic loading function __autoload() takes effect.
try-catch cannot be used to catch exceptions and errors, such as errors triggered by trigger_error(). Exceptions and errors are different.