Detailed Explanation of Error Handling and Exception Handling Method of Based on PHP7

  • 2021-09-04 23:45:16
  • OfStack

PHP7 Error Handling

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

This Error exception can be caught by the first matching try/catch block like Exception exception 1. If there is no matching catch block, the exception handler (previously registered with set_exception_handler ()) is called to handle it. If the exception handler has not been registered, it is handled the traditional way: it is reported as a fatal error (Fatal Error).

The Error class does not inherit from the Exception class, so catch (ES24e)... cannot be used to capture Error. You can use catch (Errore) {…} or catch Error by registering the exception handler (set_exception_handler ()).

Error hierarchy


Throwable
 Error
  ArithmeticError
   DivisionByZeroError
  AssertionError
  ParseError
  TypeError
 Exception
  ...

try
{
 // Code that may throw an Exception or Error.
}
catch (Throwable $t)
{
 // Executed only in PHP 7, will not match in PHP 5
}
catch (Exception $e)
{
 // 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
php 7.1
try {
 // Code that may throw an Exception or ArithmeticError.
} catch (ArithmeticError | Exception $e) {
 // pass
}

Extension (extend) PHP built-in exception handling class

Users can extend PHP's built-in exception handling classes with custom exception handling classes. The following code shows which properties and methods are accessible and inherited in subclasses in the built-in exception handling class.

Example # 1 Built-in Exception Handling Class


<?php
class Exception
{
 protected $message = 'Unknown exception'; //  Exception information 
 private $string;       // __toString cache
 protected $code = 0;      //  User-defined exception code 
 protected $file;       //  File name where exception occurred 
 protected $line;       //  Line number of code where the exception occurred 
 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 exception information 
 final public function getCode();   //  Returns the exception code 
 final public function getFile();   //  Returns the file name where the exception occurred 
 final public function getLine();   //  Returns the line number of code where the exception occurred 
 final public function getTrace();   // backtrace()  Array 
 final public function getPrevious();  //  Previous  exception
 final public function getTraceAsString(); //  That has been lattice into a string  getTrace()  Information 
 // Overrideable
 public function __toString();    //  Output string 
}
?>
 If you extend the built-in exception handling class with a custom class and want to redefine the constructor, it is recommended to call the  parent::__construct()  To check whether all variables have been assigned values. When the object is to output a string, you can overload the  __toString()  And customize the style of the output. 
 Note:
 Exception  Object cannot be copied. Try to correct  Exception  Object replication   Will result in 1 A  E_ERROR  Level error.  

<?php
/**
 *  Customize 1 Exception handling classes 
 */
class MyException extends Exception
{
 //  Redefine the constructor so that  message  Become a property that must be specified 
 public function __construct($message, $code = 0, Exception $previous = null) {
  //  Custom code 
  //  Ensure that all variables are assigned correctly 
  parent::__construct($message, $code, $previous);
 }
 //  Customize the style of string output 
 public function __toString() {
  return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
 }
 public function customFunction() {
  echo "A custom function for this type of exception\n";
 }
}

/**
 *  Create 1 Classes for testing exception handling mechanisms 
 */
class TestException
{
 public $var;
 const THROW_NONE = 0;
 const THROW_CUSTOM = 1;
 const THROW_DEFAULT = 2;
 function __construct($avalue = self::THROW_NONE) {
  switch ($avalue) {
   case self::THROW_CUSTOM:
    //  Throw a custom exception 
    throw new MyException('1 is an invalid parameter', 5);
    break;
   case self::THROW_DEFAULT:
    //  Throw the default exception 
    throw new Exception('2 is not allowed as a parameter', 6);
    break;
   default: 
    //  In the absence of exceptions, create 1 Objects 
    $this->var = $avalue;
    break;
  }
 }
}

Related articles: