PHP errors and exceptions notes and summaries (16) custom Exception handlers

Source: Internet
Author: User

You can use the custom exception handler to handle all uncaught exceptions (exceptions that are not caught with Try/catch).

Set_exception_handler (): Sets a user-defined exception handler that, when an uncaught exception occurs, the name of the function that is called (the callback function), which must be called in the

Set_exception_handler () has been defined before. The handler needs to accept a parameter, which is a thrown exception object

Cases

1<?PHP2 Header(' content-type:text/html; Charset=utf-8 ');3 Header(' content-type:text/html; Charset=utf-8 ');4 functionExceptionhandler_1 ($e){5     Echo' Custom Exception Processor 1 ';6     Echo' Exception information: '.$e-getMessage ();7 }8 9 functionExceptionhandler_2 ($e){Ten     Echo' Custom Exception Processor 2 '; One     Echo' Exception information: '.$e-getMessage (); A } -  - Throw New Exception(' Test ');

Post-run output:

Fatal error: The exception "Exception" was not caught. You can use the Set_exception_hanlder () method when an uncaught exception occurs:

1<?PHP2 Header(' content-type:text/html; Charset=utf-8 ');3 Header(' content-type:text/html; Charset=utf-8 ');4 functionExceptionhandler_1 ($e){5     Echo' Custom Exception Processor 1 ',__function__, ' <br> ';6     Echo' Exception information: '.$e-getMessage ();7 }8 9 functionExceptionhandler_2 ($e){Ten     Echo' Custom Exception Processor 2 ',__function__, ' <br> '; One     Echo' Exception information: '.$e-getMessage (); A } -  - set_exception_handler (' exceptionhandler_1 '); the Throw New Exception(' Test ');

Post-run output:

The previous example uses a custom exception handler to handle an uncaught exception.

Example 2 when more than one Set_exception_handler () method is defined, the last method overwrites the previous method, while using Restore_exception_handler () reverts to the last defined custom handler function.

1<?PHP2 Header(' content-type:text/html; Charset=utf-8 ');3 Header(' content-type:text/html; Charset=utf-8 ');4 functionExceptionhandler_1 ($e){5     Echo' Custom Exception Processor 1 ',__function__, ' <br> ';6     Echo' Exception information: '.$e-getMessage ();7 }8 9 functionExceptionhandler_2 ($e){Ten     Echo' Custom Exception Processor 2 ',__function__, ' <br> '; One     Echo' Exception information: '.$e-getMessage (); A } -  - Set_exception_handler(' exceptionhandler_1 '); the Set_exception_handler(' exceptionhandler_2 '); - Throw New Exception(' Test ');

Output:

1<?PHP2 Header(' content-type:text/html; Charset=utf-8 ');3 Header(' content-type:text/html; Charset=utf-8 ');4 functionExceptionhandler_1 ($e){5     Echo' Custom Exception Processor 1 ',__function__, ' <br> ';6     Echo' Exception information: '.$e-getMessage ();7 }8 9 functionExceptionhandler_2 ($e){Ten     Echo' Custom Exception Processor 2 ',__function__, ' <br> '; One     Echo' Exception information: '.$e-getMessage (); A } -  - Set_exception_handler(' exceptionhandler_1 '); the Set_exception_handler(' exceptionhandler_2 '); - Restore_exception_handler (); - Throw New Exception(' Test ');

Output:

After using a custom exception handler, the program does not continue to execute down:

1<?PHP2 Header(' content-type:text/html; Charset=utf-8 ');3 Header(' content-type:text/html; Charset=utf-8 ');4 functionExceptionhandler_1 ($e){5     Echo' Custom Exception Processor 1 ',__function__, ' <br> ';6     Echo' Exception information: '.$e-getMessage ();7 }8 9 functionExceptionhandler_2 ($e){Ten     Echo' Custom Exception Processor 2 ',__function__, ' <br> '; One     Echo' Exception information: '.$e-getMessage (); A } -  - Set_exception_handler(' exceptionhandler_1 '); the Throw New Exception(' Test '); -  - Echo' Continue ... ';

Output:

These are custom exception handlers to handle uncaught exceptions, and you can also use custom exception handling classes to handle uncaught exceptions

1<?PHP2 classexceptionhandler{3     protected $_exception;4     protected $_logfile= ' D:/practise/php/error/logexception2.log ';5      Public function__construct (Exception $e){6         //Save Exception Object7         $this->_exception =$e;8     }9 Ten      Public Static functionHandleException $e){ One         $self=NewSelf$e); A         $self-Log(); -         Echo $self; -     } the  -     //Logging Method -      Public function Log(){ -         Error_log($this->_exception->getmessage ().Php_eol, 3,$this-_logfile); +     } -  +      Public function__tostring () { A         $message= <<<EOF at<! DOCTYPE html> - - -<meta charset= "UTF-8" > -<title>Document</title> - in<body> - to<p> Contact your Administrator </p> +</body> - the EOF; *     return $message; $     }Panax Notoginseng } -  the  + Set_exception_handler(Array(' Exceptionhandler ', ' handle '));//handles an uncaught exception. First parameter class name, second parameter method name A //Test the Throw New Exception("Test a custom exception handler");

Output:

File LogException2.log:

PHP errors and exceptions notes and summaries (16) custom Exception handlers

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.