Errors vs Exceptions
PHP has both Errors (engine-level: TypeError, ParseError) and Exceptions (application-level). PHP 7+ made most fatal errors catchable as Error objects via the Throwable interface.
PHP has both Errors (engine-level: TypeError, ParseError) and Exceptions (application-level). PHP 7+ made most fatal errors catchable as Error objects via the Throwable interface.
<?php
// Throwable hierarchy:
// Throwable (interface)
// Error (engine errors)
// TypeError
// ParseError
// ArithmeticError
// DivisionByZeroError
// ValueError
// Exception (application errors)
// LogicException
// RuntimeException
// ...
// Catch both Error and Exception with Throwable
try {
$result = 10 / 0;
} catch (DivisionByZeroError $e) {
echo "Math error: " . $e->getMessage();
}
// Type error
try {
function strictAdd(int $a, int $b): int { return $a + $b; }
strictAdd("not", "numbers"); // TypeError in strict mode
} catch (TypeError $e) {
echo "Type error: " . $e->getMessage();
}
Catch Throwable instead of Exception to handle both PHP engine errors and application exceptions in one block.