The following small series for you to share an article based on PHP7 error handling and Exception handling method (detailed), has a good reference value, I hope to be helpful to everyone. Let's take a look at it with a little knitting.
PHP7 Error Handling
PHP 7 Changes the way most errors are reported. Unlike the Legacy (PHP 5) Error reporting mechanism, most errors are now thrown as error exceptions.
This Error exception can be captured as a Exception exception by the first matching try/catch block. If there is no matching catch block, the exception handler is called (prior to Set_exception_handler () registration) for processing. If the exception handler has not been registered, it is handled in the traditional way: it is reported as a fatal error (Fatal error).
The Error class does not inherit from the Exception class, so you cannot use catch (Exception e) ... To capture the error. You can catch the Error by catching (Errore) {...}, or by registering the exception handler (Set_exception_handler ()).
Error hierarchy
Throwable Error arithmeticerror pisionbyzeroerror assertionerror parseerror TypeError Exception ...
try{//Code that could throw an Exception or Error.} catch (Throwable $t) {//Executed only in PHP 7, would not match in PHP 5}catch (Exception $e) {//Executed only in PHP 5, Won't is reached in PHP 7}updown9lubaev dot ka at gmail dot com¶11 months agophp 7.1try {//Code ' may throw an Ex Ception or Arithmeticerror.} catch (Arithmeticerror | Exception $e) {//pass}
Extended (Extend) PHP built-in exception handling class
Users can use custom exception handling classes to extend PHP's built-in exception handling classes. The following code shows which properties and methods are accessible and inherited in the subclass of the built-in exception-handling class.
Example #1 built-in exception handling classes
<?phpclass exception{protected $message = ' Unknown Exception ';//exception information private $string; __tostring Cache protected $code = 0; User-defined exception code protected $file; The file name of the exception that occurred protected $line; The code line number where the exception occurred private $trace; BackTrace private $previous; Previous exception if nested exception public function __construct ($message = null, $code = 0, exception $previous = Nu ll); Final Private Function __clone (); Inhibits cloning of exceptions. Final public Function getMessage (); Returns the exception information final public function getcode (); Returns the exception code final public function GetFile (); Returns the file name of the exception that occurred final public function getLine (); Returns the line number of the code where the exception occurred. Final public function gettrace (); BackTrace () array final public function getprevious (); Previous exception final public function gettraceasstring (); Gettrace () information that has been rasterized into a string//overrideable Public Function __tostring (); Output string}?> If you use a custom class to extend the built-in exception-handling class, and you want to redefine the constructor, it is recommended that you call Parent::__construct () at the same time to check that all variables have been assigned values. When an objectTo output a string, you can overload __tostring () and customize the style of the output. The Note:exception object cannot be copied. Attempting to copy a Exception object causes an E_error level of error.
<?php/** * Customizing an exception handling class */class MyException extends exception{//redefine the constructor to make the message a property that must be specified public function __construct ( $message, $code = 0, Exception $previous = null) {//Custom code//Make sure all variables are correctly assigned Parent::__construct ($message, $code, $prev IOUs); }//Custom string output style public function __tostring () {return __class__. ": [{$this->code}]: {$this->message}\n"; } public Function Customfunction () {echo ' A Custom function for this type of exception\n ';}} /** * Create a class to test the exception handling mechanism */class testexception{public $var; const THROW_NONE = 0; Const THROW_CUSTOM = 1; Const THROW_DEFAU LT = 2; function __construct ($avalue = self::throw_none) {switch ($avalue) {case Self::throw_custom://Throw custom exception throw n EW MyException (' 1 is an invalid parameter ', 5); Break Case Self::throw_default://throws the default exception throw new Exception (' 2 is not allowed as a parameter ', 6); Break Default://Without exception, create an object $this->var = $avalue; Break } }}
Above this article based on PHP7 error handling and Exception handling method (detailed) is the small part to share all the content of everyone, I hope to give you a reference, but also hope that we support PHP Chinese network.
Articles you may be interested in:
Examples of pre-defined variables for PHP learning
PHP Gets an example of the start date and end date of the first few weeks of the year
PHP interface Multi-Inheritance and tarits method to realize multi-inheritance effect _php tips