Exception
The base class for all exceptions in PHP is Exception. It is used when something goes wrong in the code that should be handled gracefully.
try { throw new Exception("Something went wrong!"); } catch (Exception $e) { echo "Caught exception: " . $e->getMessage(); } // Caught exception: Something went wrong!
ErrorException
An ErrorException is thrown when a standard PHP error (like a warning or notice) is converted into an exception. You can set this behavior using set_error_handler().
function errorToException($severity, $message, $file, $line) { throw new ErrorException($message, 0, $severity, $file, $line); } set_error_handler("errorToException"); try { echo $undefinedVar; } catch (ErrorException $e) { echo "ErrorException caught: " . $e->getMessage(); } // ErrorException caught: Undefined variable $undefinedVar
ClosedGeneratorException
Thrown when you attempt to interact with a closed generator.
function gen() { yield 1; } $generator = gen(); $generator->next(); $generator->next(); // generator closes try { $generator->send("value"); // invalid on closed generator } catch (ClosedGeneratorException $e) { echo "Caught ClosedGeneratorException: " . $e->getMessage(); }
Error
Error is the base class for all internal PHP errors. Unlike Exception, it represents fatal issues in code execution.
try { nonExistingFunction(); } catch (Error $e) { echo "Caught Error: " . $e->getMessage(); } // Caught Error: Call to undefined function nonExistingFunction()
ArgumentCountError
Thrown when a function is called with too few or too many arguments.
function greet($name) { return "Hello, $name!"; } try { echo greet(); // missing argument } catch (ArgumentCountError $e) { echo "Caught ArgumentCountError: " . $e->getMessage(); } // Caught ArgumentCountError: Too few arguments to function greet(), 0 passed
ArithmeticError
Occurs when an invalid arithmetic operation happens.
try { $result = intdiv(PHP_INT_MIN, -1); // invalid integer division } catch (ArithmeticError $e) { echo "Caught ArithmeticError: " . $e->getMessage(); } // Caught ArithmeticError: Division of PHP_INT_MIN by -1 is not an integer
AssertionError
Thrown when an assertion fails (if assertions are enabled).
ini_set('assert.exception', 1); try { assert(false, "Assertion failed!"); } catch (AssertionError $e) { echo "Caught AssertionError: " . $e->getMessage(); } // Caught AssertionError: Assertion failed!
DivisionByZeroError
Thrown when dividing an integer by zero.
try { $x = intdiv(10, 0); } catch (DivisionByZeroError $e) { echo "Caught DivisionByZeroError: " . $e->getMessage(); } // Caught DivisionByZeroError: Division by zero
CompileError
Occurs when the PHP engine encounters a compile-time error. These are rare and usually unrecoverable.
try { eval('function test( {'); // invalid syntax at compile time } catch (CompileError $e) { echo "Caught CompileError: " . $e->getMessage(); } // Caught CompileError: syntax error, unexpected token "{", expecting variable
ParseError
Thrown when eval() encounters a parsing error.
try { eval('echo "Missing semicolon"'); // syntax error } catch (ParseError $e) { echo "Caught ParseError: " . $e->getMessage(); } // Caught ParseError: syntax error, unexpected end of file, expecting "," or ";"
TypeError
Thrown when an argument or return value does not match a type declaration.
function square(int $x): int { return $x * $x; } try { echo square("hello"); // wrong type } catch (TypeError $e) { echo "Caught TypeError: " . $e->getMessage(); } // Caught TypeError: square(): Argument #1 ($x) must be of type int, string given
ValueError
Introduced in PHP 8, thrown when a function receives an argument of the correct type but with an invalid value.
try { $str = substr("hello", -10, -5); // invalid value } catch (ValueError $e) { echo "Caught ValueError: " . $e->getMessage(); }
UnhandledMatchError
Thrown when a match expression does not handle all possible values.
try { $x = 5; $result = match ($x) { 1 => "One", 2 => "Two", }; } catch (UnhandledMatchError $e) { echo "Caught UnhandledMatchError: " . $e->getMessage(); } // Caught UnhandledMatchError: Unhandled match case 5
FiberError
Introduced in PHP 8.1, this occurs when there's invalid use of Fibers (lightweight concurrency).
$fiber = new Fiber(function () { return "Hello from Fiber!"; }); try { $fiber->resume(); $fiber->resume(); // invalid, already finished } catch (FiberError $e) { echo "Caught FiberError: " . $e->getMessage(); } // Caught FiberError: Cannot resume a fiber that is not suspended
RequestParseBodyException
Thrown in request_parse_body() when the request body is invalid, according to the Content-Type header.
try { throw new RequestParseBodyException("Invalid JSON body"); } catch (RequestParseBodyException $e) { echo "Caught RequestParseBodyException: " . $e->getMessage(); } // Caught RequestParseBodyException: Invalid JSON body
Source: Orkhan Alishov's notes