An Exception is a PHP object that represents a problem your program can catch and handle. You create one with throw new Exception("..."), catch it with catch (Exception $e), then read details such as the message, code, file, and stack trace.
Use catch (Exception $e) for normal application exceptions. In PHP 7+, you can also catch Throwable to handle both Exception and Error objects.
Exceptions cannot be cloned.
clone $e raises an Error. Rethrow the same instance, or wrap it with a new Exception and pass the old one as previous.
Syntax
PHP Syntax
The usual throw, catch, and inspect pattern:
PHP
try {
throw new Exception("Something went wrong", 100);
} catch (Exception $e) {
$e->getMessage(); // string message
$e->getCode(); // int code
$e->getFile(); // file where it was created
$e->getLine(); // line where it was created
$e->getPrevious(); // previous Throwable or null
$e->getTrace(); // stack as array
$e->getTraceAsString(); // stack as string
echo $e; // calls __toString()
}
// Chain a previous cause
throw new Exception("Outer failure", 1, $previous);
Reference
Core Ideas
What to remember when you work with Exception in PHP:
Idea
Example
Remember
Throw
throw new Exception("...")
Stops normal flow and jumps to a matching catch
Catch
catch (Exception $e)
Handles the object; use Throwable to catch Errors too
Message
$e->getMessage()
Human-readable text for logs or UI
Code
$e->getCode()
Your own integer category; default is 0
Location
getFile(), getLine()
Where the exception was created, not where it was caught
Chaining
new Exception($msg, $code, $prev)
Keep the root cause behind a friendlier outer message
Stack
getTrace(), getTraceAsString()
Array for code, string for quick logs
No cloning
clone $e
Raises an Error; wrap or rethrow instead
Code
PHP Examples
Three short scripts that show how Exception works in PHP. Replace the messages with your own.
Example 1 — Throw and catch
Throw an Exception with a message and code, then read both from the catch block.
PHP
<?php
try {
throw new Exception("User id is missing", 400);
} catch (Exception $e) {
echo $e->getCode(), PHP_EOL;
echo $e->getMessage(), PHP_EOL;
}
?>
Exception is the base class for errors you can catch during normal program flow. You throw an Exception with throw new Exception(...), catch it with catch (Exception $e), then read details with methods such as getMessage() and getCode().
Both implement Throwable and can be thrown or caught. Exception is for recoverable application problems. Error is for serious runtime problems such as TypeError. Beginners usually start with Exception in try and catch.
Call getMessage() on the caught object. For a longer developer report that includes the stack, cast the exception to a string or call __toString() / getTraceAsString().
When you catch one Throwable and throw a new Exception, pass the old one as the third constructor argument. Later getPrevious() returns that earlier cause so logs keep both the friendly message and the root error.
No. Exception::__clone() is final and private, so clone $e raises an Error. Rethrow the same object or create a new Exception that wraps the old one.
Practice the examples here, then open the Exception Methods index for all 10 method guides, from __construct() and getMessage() to getTraceAsString() and __clone().