Interpreting the process and principles of PHP exception mechanism

Source: Internet
Author: User
Tags try catch
What is the principle of the PHP exception mechanism? What is the ZEND_HANDLE_EXCEPTION at the end of every oparray that can be executed independently in PHP? Let's start with a question. For the code below, onError is executed, but onException is not executed.

What is the principle of the PHP exception mechanism? What is the ZEND_HANDLE_EXCEPTION at the end of each op array that can be executed independently in PHP? Let's start with a question.

For the code, onError is executed, but onException is not executed. why?

 
 
  1.  
  2. Function onError ($ errCode, $ errMesg, $ errFile, $ errLine ){
  3. Echo "Error Occurred \ n ";
  4. Throw new Exception ($ errMesg );
  5. }
  6. Function onException ($ e ){
  7. Echo $ e->GetMessage ();
  8. }
  9. Set_error_handler ("onError ");
  10. Set_exception_handler ("onException ");
  11. /* I will never name the file with my name, so this file does not exist */
  12. Require ("laruence. php ");

Running result:

 
 
  1. Error Occurred  
  2. PHP Fatal error: main(): Failed opening required 'laruence.php 

First, we need to know that when Require contains a problem that cannot be found, two errors will be thrown before and after:

1. WARNING: thrown when PHP tries to open this file.

2. E_COMPILE_ERROR: it is thrown after the function of opening the file from PHP fails to return.

As we know, set_error_handler cannot catch the E_COMPILE_ERROR error. Therefore, in onError, only the first WARNING error can be caught, but the exception thrown in onError is not caught by the default exception_handler? Let's talk about the abnormal mechanism of PHP.

Everyone who knows opcode knows that before PHP5.3, the last opcode of each op array (File, function, method) that can be run independently is ZEND_HANDLE_EXCEPTION, what is the opcode used?

In PHP, when an exception is throw, it will jump to the last line of each op array to execute this ZEND_HANDLE_EXCEPTION. the pseudo code is as follows:

 
 
  1. Void on_throw_exception (zval * exception TSRMLS_DC ){
  2. 1. determine whether an exception has been thrown
  3. 2. record exception
  4. 3. record the sequence number of the next op line to be executed
  5. 4. the next op line number to be executed = the last one of the current op array
  6. }

Just like rewriting the ip register, rewriting the serial number of the next op line to be executed changes the flow of the program, and enters the processing logic of ZEND_HANDLE_EXCEPTION. in ZEND_HANDLE_EXCEPTION, it determines whether the exception is in try catch.

◆ If yes, set the next op line to be executed as the op line of the first catch and continue the execution.

◆ If not, destroy unnecessary variables and opline and directly end the execution process.

Some people have to ask: "When will the default processing function (user_exception_handler) of the exception set by set_exception_handler take effect ?" The default exception handling function is determined after the execution is complete and exit the execution LOOP. If yes, the default exception handling function is called:

 
 
  1. // Execute
  2. Zend_execute (EG (active_op_array) TSRMLS_CC );
  3. If (EG (exception )){
  4. If (EG (user_exception_handler )){
  5. Call the user-defined default exception handling function
  6. }
  7. Else {
  8. Uncaptured exceptions
  9. }
  10. }
  11. Else {
  12. No exception
  13. }
  14. Destroy_op_array (EG (active_op_array) TSRMLS_CC );
  15. Efree (EG (active_op_array ));

PHP exception process

498) this. style. width = 498; "border =" 0 "/>

Note: One of the points in the figure is not rigorous, that is, when determining whether the last catch block is used, it will be judged at the same time (is_a). if so, it will be executed in the last catch block.

When PHP encounters a Fatal Error, zend_mongolout is directly used, and zend_mongolout causes the program process to directly skip the above code segment. It can also be understood as directly exit (longjmp ), this leads to the failure of user_exception_handler. After learning about this, why do I want to start with the article? Is that clear?

Finally, you may have some questions about ZEND_HANDLE_EXCEPTION: if so, why does the ZEND_HANDLE_EXCEPTION exist at the end of every independently executed op array? The simplest one is that if a function does not throw, this opcode is obviously unnecessary? You are very smart. PHP 5.3 has been adjusted according to your ideas. ZEND_HANDLE_EXCEPTION opline is dynamically generated only at the throw moment.

Link: http://www.laruence.com/2010/08/03/1697.html

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.