PHP ErrorException

PHPServer Side ProgrammingProgramming


PHP's Exception class implements the Throwable interface. ErrorException class extends the Exception class. ErrorException is meant to be explicitly thrown when you want to catch and handle errors that would otherwise be ignored, such as Notices or Warnings.

PHP core consists of following predefined error constants

1E_ERRORFatal run-time errors.
2E_WARNINGRun-time warnings (non-fatal errors).
4E_PARSECompile-time parse errors.
8E_NOTICERun-time notices.
16E_CORE_ERRORFatal errors that occur during PHP's initial startup.
32E_CORE_WARNINGWarnings (non-fatal errors) that occur during PHP's initial startup.
64E_COMPILE_ERRORFatal compile-time errors.
128E_COMPILE_WARNINGCompile-time warnings (non-fatal errors).
256E_USER_ERRORUser-generated error message.
512E_USER_WARNINGUser-generated warning message.
1024E_USER_NOTICEUser-generated notice message.
2048E_STRICTIf Enabled PHP suggests changes to your code to ensure interoperability and forward compatibility of your code.
4096E_RECOVERABLE_ERRORCatchable fatal error.
8192E_DEPRECATEDRun-time notices.
16384E_USER_DEPRECATEDUser-generated warning message.
32767E_ALLAll errors and warnings, E_STRICT

In addition to properties and methods inherited from Exception class, ErrorException class introduces one property and one method as follows −

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

The severity of exception is represented by integer number associated with type of error in above table

ErrorException example

In following script, a user defined function errhandler is set as Error handler with set_error_handler() function. It throws ErrorException when fatal error in event of file not found for reading is encountered.


 Live Demo

function errhandler($severity, $message, $file, $line) {
   if (!(error_reporting() & $severity)) {
      echo "no error";
   throw new ErrorException("Fatal Error:No such file or directory", 0, E_ERROR);
/* Trigger exception */
   echo $data;
catch (ErrorException $e){
   echo $e->getMessage();

Above example displays following output


Fatal Error:No such file or directory
Published on 21-Sep-2020 14:55:22