PHP error and exception handling summary

Source: Internet
Author: User
Tags php error
PHP errors and exception handling summary

PHP has a number of built-in functions related to error and exception handling. This article will detail some of these functions.

Http://leo108.com

Set_error_handler and restore_error_handler

Set_error_handler can set the corresponding error to be handed over to the user-defined logic for processing when an error occurs in the program. Leo108's blog

However, not all errors can be captured by the processing logic specified by set_error_handler, such as E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, and E_COMPILE_WARNING, simply put, errors that would have caused program termination cannot be captured. PHP error and exception handling summary

Set_error_handler can only set one error handling logic. multiple calls to set_error_handler only take effect for the last time:

Leo108's blog

set_error_handler(function($errno, $errstr){    echo 'catch error1';});set_error_handler(function($errno, $errstr){    echo 'catch error2';});trigger_error('something error');

The output is the leo108's blog.

Catch error2

Http://leo108.com

When writing a third-party code library, if you want to capture errors in the code library without affecting the caller's handling of errors, you can use the restore_error_handler function: PHP

Function third_party_function () {// error handling logic of the third-party class library set_error_handler (function () {// some code }); // logic of the third-party class library // some code // restore the error handling logic restore_error_handler ();}

In this way, errors in the third-party code library will be captured by the error processing logic of the third-party code library, and errors outside the third-party code library will still be handled by the original logic.

Exception

If the return value of the error processing logic is false, PHP will call the built-in error processing logic (for example, determine whether to print error information and write error logs based on the value of error_reporting ); if the return value is true, the built-in processing logic will not be called. after the custom error processing logic is executed, the system will return the triggering error and continue to execute it.

Pushing cool is a shameless website

The error_reporting and @ symbols do not affect the call to the error handling logic:

Pushing cool is a shameless website

error_eporting(0);set_error_handler(function($errno, $errstr){    echo 'catch error';});@trigger_error('something error');

The above code will output:

PHP

Catch error

Leo108's blog

Only E_ERROR and E_PARSE are common errors that cannot be captured by set_error_handler. The former may be because the memory usage exceeds the set value, a nonexistent class is instantiated, or a non-existent function is called. The latter is usually caused by a syntax parsing error, for example, a semicolon is missing. For E_PARSE, there is no way to capture and handle errors at the syntax level. However, for E_ERROR, you can use register_shutdown_function and error_get_last functions. the sample code is as follows:

Pushing cool is a shameless website

error_reporting(0);register_shutdown_function(function() { $error = error_get_last(); if ($error != null && $error['type'] == E_ERROR) {echo "fatal error catched:" . var_export($error, true); }});new test();

Output:

Http://leo108.com

Fatal error catched: array (

Http://leo108.com

'Type' => 1. PHP errors and exception handling summary

'Message' => 'class' test' not found '. Summary of PHP errors and exception handling

'File' => '/tmp/error. php ',

Pushing cool is a shameless website

'Line' => 20,

PHP

)

Leo108's blog

Set_exception_handler and restore_exception_handler

These two functions are similar to those of set_error_handler. Likewise, only one exception processing logic can be registered. only the last one takes effect for multiple registrations. you can use restore_exception_handler to restore the previous exception processing logic. Leo108's blog

Some small experiments trigger errors in the error handling logic
set_error_handler(function($errno, $errstr){    echo 'catch error:' . $errstr . PHP_EOL;    trigger_error('error in error handler');});trigger_error('origin error');

Output: leo108's blog

Catch error: origin error

Exception

PHP Notice: error in error handler in/tmp/error. php on line 15

PHP error and exception handling summary

Conclusion: errors in the error processing logic cannot be captured again. Exception

Throw an exception in the exception handling logic
set_exception_handler(function(Exception $e){    echo 'catch exception:' . $e->getMessage() . PHP_EOL;    throw new Exception('exception in exception handler');});throw new Exception('origin exception');

Output: pushing cool is a shameless website.

Catch exception: origin exception

Pushing cool is a shameless website

PHP Fatal error: Uncaught exception 'exception' with message 'exception in Exception handler' in/tmp/error. php: 15 http://leo108.com/pid-2216.asp

Stack trace: Exception

0 [internal function]: {closure} (Object (Exception) 1 [main]

Thrown in/tmp/error. php on line 15 http://leo108.com

Conclusion: The Exception thrown in the exception handling logic will not be caught by the http://leo108.com/pid-2216.asp

The exception and error handling logic are defined at the same time. An exception is thrown in the error handling logic and an error is triggered in the exception handling logic.
set_exception_handler(function(Exception $e){    echo 'catch exception:' . $e->getMessage() . PHP_EOL;    trigger_error('error in exception handler');});set_error_handler(function($errno, $errstr){    echo 'catch error:' . $errstr . PHP_EOL;    throw new Exception('exception in error handler');});
An error is triggered externally.
trigger_error('origin error');

Output:

Exception

Catch error: origin error

Http://leo108.com

Catch exception: exception in error handler

Http://leo108.com

Catch error: error in exception handler

Exception

PHP Fatal error: Uncaught exception 'exception' with message 'exception in error handler' in/tmp/error. php: 9

Leo108's blog

Stack trace:

PHP

0 [internal function]: {closure} (1024, 'error in handle t... ','/Tmp/error. php', 5, Array) 1/tmp/error. php (5): trigger error ('error in failed t... ') 2 [internal function]: {closure} (Object (Exception) 3 [main]

Thrown in/tmp/error. php on line 9

Http://leo108.com/pid-2216.asp

Conclusion: two error handling logic and one exception handling logic are called.

Pushing cool is a shameless website

An exception is thrown.
throw new Exception('origin exception');

Output:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.