Home > Article > Backend Development > Questions about PHP Exception
Generally, in order to collect PHP exception information for subsequent processing, you will set up an exception handler or inherit the base class to write an exception subclass. What I use here is a registered exception handler. I encountered several problems that I didn’t quite understand
1: After an exception is thrown, the program will terminate, but I vaguely remember seeing somewhere before that the program will not terminate after an exception is thrown under certain specific circumstances. I have forgotten this specific situation. Please provide some popular science.
2: If after registering the exception handler, the code segment tries to throw the exception, the registered function will not be executed. I don’t quite remember why this is the case. Please give me some more knowledge.
3: Regarding the design of exceptions, in order to facilitate the collection of some logs, how to collect exception logs is generally done. I registered the exception handler and then used this function to organize and collect it. But after trying, this function It is not executed, so I don’t know how to do this design of collecting logs. Please give me some popular science.
Generally, in order to collect PHP exception information for subsequent processing, you will set up an exception handler or inherit the base class to write an exception subclass. What I use here is a registered exception handler. I encountered several problems that I didn’t quite understand
1: After an exception is thrown, the program will terminate, but I vaguely remember seeing somewhere before that the program will not terminate after an exception is thrown under certain specific circumstances. I have forgotten this specific situation. Please provide some popular science.
2: If after registering the exception handler, the code segment tries to throw the exception, the registered function will not be executed. I don’t quite remember why this is the case. Please give me some more knowledge.
3: Regarding the design of exceptions, in order to facilitate the collection of some logs, how to collect exception logs is generally done. I registered the exception handler and then used this function to organize and collect it. But after trying, this function It is not executed, so I don't know how to do this design of collecting logs. Please give me some popular science.
Thinkphp's E method is exception. This is mainly used to directly throw errors and terminate.
I already know how to use try. If I catch an exception, I just need to write down what I need to do after the exception.
<code><?php try { $mgr = new CommandManager(); $cmd = $mgr->getCommandObject("realcommand"); $cmd->execute(); } catch (Exception $e) { print $e->getMessage(); exit(); } ?></code>
1. What I know so far is that if an exception is caught in try catch, then the program will not terminate.
2. There are instructions for set_exception_handler in the manual.
Set the default exception handler. It is used to catch without try/catch block. Abnormal . Exceptions are aborted after exception_handler is called.
3. Since catch captures the exception, exception_handler naturally cannot collect the exception in the corresponding code segment.
function _exception_handler($exception)
<code>{ $_error =& load_class('Exceptions', 'core'); $_error->log_exception('error', 'Exception: '.$exception->getMessage(), $exception->getFile(), $exception->getLine()); // Should we display the error? if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) { $_error->show_exception($exception); } exit(1); // EXIT_ERROR } </code>
The above is the custom exception function that comes with the CI framework, so the log is nothing more than collecting getMessage and the code file and line number where the error occurred. For example, CI can also capture the controller and action when the code occurs
Exception is a more complete error handling mechanism. Compared with errors, exceptions have a more complete stack processing method.
This also determines that after handling the exception, the program will not return to the original position and continue execution (this is okay for errors).
Why the execution cannot continue? This is caused by the different handling mechanisms of exceptions and errors:
Error handling is to interrupt at the location where the error occurs, jump to the error handling function, and then jump back to interrupt after the processing is completed, so it can Continue execution.
Exception handling is to push up the stack and find the program segment that can catch the exception to handle the exception. Because the stack has been cleared, it is no longer possible to return to the original execution position.
The exception handler function is a function created by PHP to be compatible with functional programming. But this actually does not conform to the standard exception handling method. Exception handling is best implemented using try {} catch {}.
As for what you said if you want the exception to break through the inner catch and be caught by the outer catch, you can use throw $e in the inner catch.