In learning, the more learning the more you feel weak foundation.
In normal work, for some error handling feel do not know how to do, so decided to re-organize.
Strongly recommend this article, really feel a lot of learning.
Some references:: Another comment on PHP error and exception handling
--
1. What is error and exception handling in PHP?
In my understanding.
Any execution plan that does not conform to the program's expectations is an exception.
is a written issue that causes the compiler to fail the compilation. or something. is not captured by Try-cache.
My understanding is really very shallow, the above article to this aspect explanation is very detailed.
--
2, the wrong level
This question raises the reason why I have to re-learn the error and exception handling today.
BOOL Trigger_error (String $error _msg [, int $error _type = E_user_notice]) produces a user-level error/warning/notice information
This function raises the concept of the error level, and I was thinking, what is a user-level error?
This leads to the classification of the error level, let's see what the level of error is.
Fatal ERR: Fatal error (script terminated run) e_error //Fatal run error, error unrecoverable, pause execution script e_core_error //PHP startup fatal error during initialization _compile_error//compile-time fatal error, just like generated by the Zend script engine a e_error e_user_error //Custom fault message. Like PHP function trigger_error (Error type set to: E_user_error) Parse Error: Compile-time parsing errors, syntax errors (script termination run) E_parse //compile-time parsing errors Warning Error: Warning errors (informational only, script does not terminate) E_warning //Run-time warning (non-fatal error). e_core_warning //PHP initialization warning (non-fatal error) occurred during startup. e_compile_warning//Compile warning E_user_warning //user-generated warning Notice error: Notification errors (only given notification information, script does not terminate) E_notice //Runtime notifications. Indicates that the script encountered a situation that might appear to be an error. E_user_notice//user-generated notification information.
These error messages will be reflected in the log, which reflects my problem above.
What is a user-level error message.
Is that the user has customized a fatal error.
Knowing these things, we can go on and on.
--
3. PHP Exception Handling function
Here is also the study and record.
One: Set_error_handler ()
Look at the official explanation for him: set user-defined error handling functions
It's a good idea to define a function in which error handling occurs.
Mixed Set_error_handler (callable $error _handler [, int $error _types = E_all | E_strict])
Note, however , that this function only captures some warning, notice-level errors that are generated by the system.
II: Register_shutdown_function ()
look at the official explanation for him: Register a function that will execute when PHP is aborted
This is also literally understandable, which is the function called when PHP is aborted.
void Register_shutdown_function (callable $callback [, Mixed $parameter [, mixed $ ...])
Note, however, that PHP on a single page will not trigger errors when testing, and must be introduced through Include/require to take effect.
Specific or look at the great God's article: ask what can not be triggered, why is it possible in the framework?
Three:Set_exception_handler ()
look at the official explanation for him: sets the default exception handler for exceptions that are not caught with a try/catch block.
Callable Set_exception_handler (callable $exception _handler)
--
4. Custom handler functions and exception nesting
In normal use, this is probably the most used, because this part has been encapsulated before using the frame, but we want to have a corresponding processing mechanism at the corresponding time, it will continue to expand.
One: Custom exception handling
In complex systems, we often need to capture our own exceptions that require special handling, which may be thrown in special cases. So we define an exception-capturing class ourselves, which must be an extension of the exception class that inherits all the properties of the PHP exception class, and we can add custom functions that, when used in the same way as before, are roughly written as follows:
<?phpclass Go extends exception{public function M () { return ' Error line '. $this->getline (). $this->getfile () . ': <b> '. $this->getmessage (); } } try { throw new Go ($age);} catch (Go $e) { echo $e->m ();}
Two: Custom exception handling
Here is a part of the code to borrow the big guy, in general, can be nested multiple try-cache in the catch error, layered pass the exception.
Note: When there are multiple exceptions, the first exception is handled preferentially
<?php class Zyfexception extends Exception {public function errorzyfmessage () { return ' Error line '. $this->getline (). ' In '. $this->getfile () . ': <b> ' $this->getmessage (). ' </b> must in (0-60) '; } } $age = +; try { $age = intval ($age); if ($age >) { throw new zyfexception ($age); } //if ($age <= 0) { throw new Exception ($age. ' must > 0 '); } } catch (Zyfexception $e) { echo $e->errorzyfmessage (); } catch (Exception $e) { echo $e GetMessage (); }? >
--
About the anomalies and errors are written today, feel really, the big guy wrote very well, very beneficial.
At the same time feel their own do not understand, more should learn more.
2018/05/02 PHP Error and exception handling