PHP ErrorException (PHP error exception)

王林
Release: 2023-08-30 18:26:01
forward
1782 people have browsed it

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
Copy after login

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();
}
?>
Copy after login

The above example shows the following output

Output

Fatal Error:No such file or directory
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template