0

Throwable vs. Exception: PHP Error Handling

If you’re a seasoned PHP developer, you’ve undoubtedly worked with Exception handling. But have you ever wondered why PHP 7 introduced Throwable? Is it just a fancy new keyword, or does it offer something substantial? Buckle up, because we’re about to dive deep into the world of error handling in PHP, and by the end of this article, you’ll have a solid grasp of when to use Throwable and when to stick with Exception.


What is Exception?

The Exception class has been the backbone of PHP error handling since PHP 5. When something goes wrong in your application—say, a failed database query or an invalid API response—you throw an exception. This allows you to gracefully handle errors rather than letting your application crash.

Example of Exception

try {
    throw new Exception("Something went wrong!");
} catch (Exception $e) {
    echo "Caught Exception: " . $e->getMessage();
}

Output:

Caught Exception: Something went wrong!

Pretty straightforward, right?


What is Throwable?

PHP 7 introduced Throwable as the top-level interface for all errors and exceptions. Before PHP 7, fatal errors (like type mismatches or syntax issues) would crash your script, and there was no way to catch them. With Throwable, you can now catch both exceptions and errors in a single try-catch block.

Example of Throwable

try {
    throw new Error("This is a fatal error!");
} catch (Throwable $t) {
    echo "Caught Throwable: " . $t->getMessage();
}

Output:

Caught Throwable: This is a fatal error!

In previous PHP versions, this would have resulted in a fatal error. But with Throwable, you now have full control over handling such situations.


Key Differences Between Throwable and Exception

FeatureExceptionThrowable
Introduced InPHP 5PHP 7
Can Catch Errors?NoYes
Inherits FromNone (Base class)Interface
Parent ClassExceptionThrowable
Includes Error?NoYes

One key takeaway: Throwable is not a class—it’s an interface that both Exception and Error implement. This means you can use Throwable to catch everything, or you can stick to Exception if you only care about logical errors.


When to Use Throwable vs. Exception

Use Exception When:

✅ You only want to catch application-specific errors, like invalid input or failed transactions.
✅ You’re working with legacy PHP 5.x code, which doesn’t support Throwable.
✅ You want to ensure that fatal errors are not caught, allowing them to crash the app for debugging purposes.

Use Throwable When:

✅ You need to catch both exceptions and errors in one block.
✅ You’re working in a PHP 7+ environment and want complete error control.
✅ You want to gracefully handle critical errors (e.g., out-of-memory errors, fatal type mismatches) without letting the script crash.


A Real-World Example

Imagine you’re building a microservice that processes incoming API requests. You want to ensure that all errors—whether they come from logic errors (Exception) or unexpected runtime issues (Error)—are logged properly.

Without Throwable

try {
    // API call simulation
    $data = json_decode("invalid_json", true, 512, JSON_THROW_ON_ERROR);
} catch (Exception $e) {
    echo "Caught Exception: " . $e->getMessage();
} catch (Error $e) {
    echo "Caught Fatal Error: " . $e->getMessage();
}

With Throwable

try {
    // API call simulation
    $data = json_decode("invalid_json", true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable $t) {
    echo "Caught Throwable: " . $t->getMessage();
}

By using Throwable, we eliminate redundancy and make our error handling cleaner and more maintainable.


Conclusion

Throwable is a game-changer in PHP error handling. It allows you to catch both exceptions and errors, making your applications more robust and resilient. If you’re working in a modern PHP 7+ codebase, Throwable should be your go-to choice for comprehensive error management. However, if you want finer control over exception handling or maintain compatibility with PHP 5, Exception still serves its purpose.

So next time you’re handling errors, think like a rockstar developer: Do you need to catch everything? Use Throwable. Need only logical errors? Stick with Exception.

Stay sharp, code smart, and happy debugging! 🚀

Latest Blog