Predefined Exceptions

Table of Contents

See also the SPL Exceptions

Exception

Introduction

Exception is the base class for all user exceptions.

Class synopsis

Exception implements Throwable
/* Properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Methods */
public Exception::__construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string Exception::getMessage()
final public Throwablenull Exception::getPrevious()
final public int Exception::getCode()
final public string Exception::getFile()
final public int Exception::getLine()
final public array Exception::getTrace()
final public string Exception::getTraceAsString()
public string Exception::__toString()
private void Exception::__clone()

Properties

message

The exception message

code

The exception code

file

The filename where the exception was created

line

The line where the exception was created

previous

The previously thrown exception

string

The string representation of the stack trace

trace

The stack trace as an array

ErrorException

Introduction

An Error Exception.

Class synopsis

ErrorException
extends Exception
/* Properties */
protected int $severity = E_ERROR;
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Methods */
public ErrorException::__construct(
    string $message = "",
    int $code = 0,
    int $severity = E_ERROR,
    stringnull $filename = null,
    intnull $line = null,
    Throwablenull $previous = null
)
final public int ErrorException::getSeverity()
/* Inherited methods */
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

Properties

severity

The severity of the exception

Examples

Example #1 Use set_error_handler to change error messages into ErrorException.

<?php
function exception_error_handler(int $errnostring $errstrstring $errfile nullint $errline) {
    if (!(
error_reporting() & $errno)) {
        
// This error code is not included in error_reporting
        
return;
    }
    throw new 
\ErrorException($errstr0$errno$errfile$errline);
}
set_error_handler(exception_error_handler(...));
// Prior to PHP 8.1.0 and the introduction of the first class callable syntax, the following call must be used instead
// set_error_handler(__NAMESPACE__ . "\\exception_error_handler");

/* Trigger exception */
strpos();
?>

The above example will output something similar to:

Fatal error: Uncaught exception 'ErrorException' with message 'strpos() expects at least 2 parameters, 0 given' in /home/bjori/tmp/ex.php:12
Stack trace:
#0 [internal function]: exception_error_handler(2, 'strpos() expect...', '/home/bjori/php...', 12, Array)
#1 /home/bjori/php/cleandocs/test.php(12): strpos()
#2 {main}
  thrown in /home/bjori/tmp/ex.php on line 12

The ClosedGeneratorException class

Introduction

A ClosedGeneratorException is thrown when trying to retrieve a value from a closed Generator.

Class synopsis

ClosedGeneratorException
extends Exception
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public __construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

Error

Introduction

Error is the base class for all internal PHP errors.

Class synopsis

Error implements Throwable
/* Properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Methods */
public Error::__construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string Error::getMessage()
final public Throwablenull Error::getPrevious()
final public int Error::getCode()
final public string Error::getFile()
final public int Error::getLine()
final public array Error::getTrace()
final public string Error::getTraceAsString()
public string Error::__toString()
private void Error::__clone()

Properties

message

The error message

code

The error code

file

The filename where the error happened

line

The line where the error happened

previous

The previously thrown exception

string

The string representation of the stack trace

trace

The stack trace as an array

ArgumentCountError

Introduction

ArgumentCountError is thrown when too few arguments are passed to a user-defined function or method.

This error is also thrown when too many arguments are passed to a non-variadic built-in function.

Class synopsis

ArgumentCountError
extends TypeError
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public Error::__construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string Error::getMessage()
final public Throwablenull Error::getPrevious()
final public int Error::getCode()
final public string Error::getFile()
final public int Error::getLine()
final public array Error::getTrace()
final public string Error::getTraceAsString()
public string Error::__toString()
private void Error::__clone()

ArithmeticError

Introduction

ArithmeticError is thrown when an error occurs while performing mathematical operations. These errors include attempting to perform a bitshift by a negative amount, and any call to intdiv that would result in a value outside the possible bounds of an int.

Class synopsis

ArithmeticError
extends Error
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public __construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

AssertionError

Introduction

AssertionError is thrown when an assertion made via assert fails.

Class synopsis

AssertionError
extends Error
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public __construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

DivisionByZeroError

Introduction

DivisionByZeroError is thrown when an attempt is made to divide a number by zero.

Class synopsis

DivisionByZeroError
extends ArithmeticError
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public Error::__construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string Error::getMessage()
final public Throwablenull Error::getPrevious()
final public int Error::getCode()
final public string Error::getFile()
final public int Error::getLine()
final public array Error::getTrace()
final public string Error::getTraceAsString()
public string Error::__toString()
private void Error::__clone()

CompileError

Introduction

CompileError is thrown for some compilation errors, which formerly issued a fatal error.

Class synopsis

CompileError
extends Error
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public __construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

ParseError

Introduction

ParseError is thrown when an error occurs while parsing PHP code, such as when eval is called.

Note: ParseError extends CompileError as of PHP 7.3.0. Formerly, it extended Error.

Class synopsis

ParseError
extends CompileError
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public Error::__construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string Error::getMessage()
final public Throwablenull Error::getPrevious()
final public int Error::getCode()
final public string Error::getFile()
final public int Error::getLine()
final public array Error::getTrace()
final public string Error::getTraceAsString()
public string Error::__toString()
private void Error::__clone()

TypeError

Introduction

A TypeError may be thrown when:

  • The value being set for a class property does not match the property's corresponding declared type.
  • The argument type being passed to a function does not match its corresponding declared parameter type.
  • A value being returned from a function does not match the declared function return type.

Class synopsis

TypeError
extends Error
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public __construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

Changelog

Version Description
7.1.0 A TypeError is no longer thrown when an invalid number of arguments are passed to a built-in PHP function in strict mode. Instead, an ArgumentCountError is raised.

ValueError

Introduction

A ValueError is thrown when the type of an argument is correct but the value of it is incorrect. For example, passing a negative integer when the function expects a positive one, or passing an empty string/array when the function expects it to not be empty.

Class synopsis

ValueError
extends Error
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public __construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

UnhandledMatchError

Introduction

An UnhandledMatchError is thrown when the subject passed to a match expression is not handled by any arm of the match expression.

Class synopsis

UnhandledMatchError
extends Error
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Inherited methods */
public __construct(string $message = "", int $code = 0, Throwablenull $previous = null)
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()

FiberError

Introduction

FiberError is thrown when an invalid operation is performed on a Fiber.

Class synopsis

final FiberError
extends Error
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private Throwablenull $previous = null;
/* Methods */
public FiberError::__construct()
/* Inherited methods */
final public string getMessage()
final public Throwablenull getPrevious()
final public int getCode()
final public string getFile()
final public int getLine()
final public array getTrace()
final public string getTraceAsString()
public string __toString()
private void __clone()