PHP7 error handling

PHP 7 changes the way most of the error is reported. Unlike the traditional (PHP 5) error reporting mechanism, most of the errors are now thrown as Error exceptions. The Error exception of

can be captured by the first matched try / catch block, like a Exception exception. If there is no matching catch block, the exception handler function is invoked (prior to set_exception_handler () registration) to be processed. If the exception processing function 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 can't use catch (Exception E) to capture Error. You can use catch (Errore) {... }, or capture Error by registering an abnormal processing function (set_exception_handler ()).


 Throwable Error ArithmeticError structure DivisionByZeroError AssertionError ParseError TypeError Exception... 
 try that may throw {/ / Code an Exception or Error. catch (Throwable $t) {} / / Executed only in PHP will not match 7, in PHP 5 (Exception $e) {catch} / / Executed only in PHP 5, will not be reached in PHP 7 up down 9 lubaev dot Ka} at Gmail dot com 11 months ago 7.1 try PHP wearing that may throw {/ / Code an Exception or ArithmeticError. catch (ArithmeticError Exception $e |}) {/ / pass}

extension the exception class

Users can use custom exception handling classes to extend the PHP's built-in exception handling class. The following code shows which attributes and methods are accessible and inherited in the subclass in the built-in exception handling class.

Example #1

 built-in exception class < PHP? Class Exception $message exception'{protected ='Unknown; / / private / / __toString anomaly information $string; cache protected $code = 0; / / user-defined exception code protected $file; / / abnormal file name protected $line; / / abnormal code line number private / / $trace; backtrace private $previous previous exception if nested; / / exception public function __construct ($message = null, $code = 0, Exception = $previous null; final private function (__clone)); / / Inhibits cloning of exceptions. final public function (getMessage); / / return final public function (getCode) anomaly information; / / return exception code f Inal public function (getFile); / / return abnormal file name final public function (getLine); / / return of abnormal final public function getTrace (line of code); / / backtrace (final) public function (getPrevious) array; / / exception final public function before getTraceAsString (); / / has lattice into into string (getTrace) public function __toString (information / / Overrideable);} / / string output? > if the extension uses a custom class to built-in exception handling, and to define the constructor, call parent:: __construct (recommended) to check whether all variables have been assigned. When the object is to output a string, the __toString () can be overloaded and the style of the output is customizable. The Note: Exception object cannot be copied. Attempting to copy Exception objects can lead to an E_ERROR level error. 
 < / * * * PHP? A custom exception class / class MyException extends Exception {/ / the message to redefine the constructor must be designated public function __construct attribute ($message, $code = 0, Exception = $previous null) {/ / / / self defined code to ensure that all variables are assigned properly: parent: __construct ($message, $code, $previous);} / / custom string output style public function __toString (return) {__CLASS__. ": [{$this-> code}]: {$this-> message}n; public (customFunction)} function {echo" A custom function for this type of exceptionn ";}} / * * * create a test the exception handling mechanism of class TestException / class {public $var; const THROW_NONE = 0; const THROW_CUSTOM Const = 1; THROW_DEFAULT = 2; function __construct ($avalue = self: THROW_NONE) {switch ($avalue) {case self:: THROW_CUSTOM: / throw new MyException throw custom exceptions ('1 is an invalid parameter', 5); break case; self:: THROW_DEFAULT: / throw new Exception throws the default exception ('2 is not allowed as a parameter', 6); break default:; / / without exception, creating an object $this-> VAR = $avalue; break}

;}} over this PHP7 error handling and exception handling method based on (explain) is small to share all your content, I hope to give you a reference, also hope that we can support a script.

This concludes the body part