Home  >  Article  >  Backend Development  >  PHP ErrorException (PHP error exception)

PHP ErrorException (PHP error exception)

王林
王林forward
2023-08-30 18:25:021610browse

PHP ErrorException(PHP错误异常)

Introduction

PHP’s Exception class implements the Throwable interface. The ErrorException class inherits the Exception class. ErrorException can be thrown explicitly when you want to catch and handle an otherwise ignored error, such as a notification or warning.

PHP core contains the following predefined error constants

Value Constant Description
1 E_ERROR Fatal runtime error.
2 E_WARNING Run-time warning (non-fatal error).
4 E_PARSE Parse error during compilation.
8 E_NOTICE Runtime notification.
16 E_CORE_ERROR A fatal error that occurred during the initial startup of PHP.
32 E_CORE_WARNING A warning (non-fatal error) that occurs during the initial startup of PHP.
64 E_COMPILE_ERROR Fatal compile-time error.
128 E_COMPILE_WARNING Compile-time warning (non-fatal error).
256 E_USER_ERROR User generated error message.
512 E_USER_WARNING User generated warning message.
1024 E_USER_NOTICE User-generated notification message.
2048 E_STRICT If enabled, PHP recommends changes to your code to ensure code interoperability and forward compatibility.
4096 E_RECOVERABLE_ERROR Catchable fatal error.
8192 E_DEPRECATED Runtime notification.
16384 E_USER_DEPRECATED User generated warning message.
32767 E_ALL All errors and warnings, E_STRICT

except from Exception In addition to the properties and methods inherited by the class, the ErrorException class also introduces a property and a method, as shown below −

protected int severity ;
final public getSeverity ( void ) : int

The severity of the exception is represented by the integer related to the error type in the above table

ErrorException Example

In the following script, the user-defined function errhandler is set as an error handler via the 通过set_error_handler() function. It throws an ErrorException when it encounters a fatal error that cannot read the file.

Example

Live Demonstration

<?php
function errhandler($severity, $message, $file, $line) {
   if (!(error_reporting() & $severity)) {
      echo "no error";
      return;
   }
   throw new ErrorException("Fatal Error:No such file or directory", 0, E_ERROR);
}
set_error_handler("errhandler");
/* Trigger exception */
try{
   $data=file_get_contents("nofile.php");
   echo $data;
}
catch (ErrorException $e){
   echo $e->getMessage();
}
?>

The above example shows the following output

Output

Fatal Error:No such file or directory

The above is the detailed content of PHP ErrorException (PHP error exception). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete