How to Catch an Exception in PHP
When code hits a problem it can't handle, it throws an exception, and the script stops unless something catches it. Wrapping the call in try and naming the exception's class in catch lets you handle the failure you expected and carry on.
Syntax
try {
// code that may throw
} catch (<ExceptionClass> $<e>) {
// handle it, for example with $<e>->getMessage()
}
| Part | What it does |
|---|---|
try { ... } | Runs the code that may throw. |
catch (<ExceptionClass> $<e>) | Runs only for that class or a class that extends it. |
$<e>->getMessage() | The message the exception was created with. |
The PHP manual on errors and exceptions: "It is possible to handle a Throwable error within PHP by surrounding the code that may throw an error in a try block." And: "When a Throwable error is thrown, it will unwind the call stack until it is caught by a catch block or the global scope is reached."
The Example
The example converts text to a number. For forty-two the function throws an InvalidArgumentException, which the catch block reports.
Prerequisites
- PHP for the command line. This guide uses PHP 8.6.0.
Step 1: Write the Code
Create a file named main.php:

<?php
function toNumber(string $text): int
{
if (!is_numeric($text)) {
throw new InvalidArgumentException("Not a number: $text");
}
return (int) $text;
}
try {
echo toNumber('42'), "\n";
echo toNumber('forty-two'), "\n";
} catch (InvalidArgumentException $e) {
echo "Caught: ", $e->getMessage(), "\n";
}
throw new InvalidArgumentException(...)creates the exception and stops the function.- The second
echoinsidetrynever runs: the exception jumps straight tocatch.
Step 2: Run It
php main.php
42
Caught: Not a number: forty-two

The command is the same in zsh, bash and PowerShell.
Catching a Parent Class
Exceptions form a family tree: InvalidArgumentException extends LogicException, which extends Exception. A catch (Exception $e) caught the InvalidArgumentException too. Catch the specific class when only that failure is expected, and a parent class when a whole family should be handled the same way. Throwable sits above both Exception and PHP's own Error classes.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
No try around a call that throws | Fatal error: Uncaught InvalidArgumentException: Not a number: forty-two and exit code 255 | Catch it where you can handle it |
Catching Exception and expecting a TypeError | TypeError is an Error, not an Exception, so it isn't caught | Catch TypeError, or Throwable |
An empty catch block | The failure disappears silently | At least log $e->getMessage() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Fatal error: Uncaught … | Nothing caught the exception | Add a try/catch for that class |
The catch block never runs | The thrown class isn't the one named, or a child of it | Catch the class named in the error message |
FAQ
Can one catch handle several classes?
Yes, with |: catch (InvalidArgumentException | TypeError $e).
Is there a finally block?
Yes. finally { ... } after the catch runs whether or not an exception was thrown.
Conclusion
Wrap risky calls in try, handle the expected class in catch, and read the reason with getMessage(). A parent class catches its children, so name the class that fits the failures you can handle.
Additional Resources
- Errors and exceptions, in the PHP manual.
- InvalidArgumentException, in the PHP manual.
