How to Catch a Fatal PHP Error (E_ERROR) and Keep Your Sanity
\n\nHave you ever seen this? Your PHP site displays a blank white page with no error at all. Or, even worse, a scary message like Fatal error: Uncaught Error.... If you are a developer working with PHP, you have encountered E_ERROR — the most unpleasant type of error. Unlike Warnings or Notices, a fatal error completely stops script execution. And the most frustrating part: the standard try...catch block does not catch it.
In this article, we will break down how to properly catch fatal PHP errors, why regular methods don't work, and how to make your code more resilient. This is not just theory — we will write several real-world examples that you can immediately apply to your projects.
\n\nWhy Doesn't try...catch Save You from E_ERROR?
\n\nLet's dispel the illusion right away: try...catch in PHP is designed to handle Exceptions. And E_ERROR is an Error. These are different entities. PHP before version 7.0 did not allow catching fatal errors at all. Starting from PHP 7, the Error class appeared, which, like Exception, implements the Throwable interface. But this does not mean that any E_ERROR automatically becomes an exception. For example, an out-of-memory error or calling a non-existent method are still fatal errors that cannot always be caught via try...catch.
Here is a simple example:
\n\ntry {\n $obj = new NonExistentClass(); // This will trigger E_ERROR\n} catch (Throwable $e) {\n echo 'Caught: ' . $e->getMessage();\n}\n\nIn PHP 7+, this code will work because Throwable includes Error. But if the error occurs during the compilation phase (e.g., a syntax error) or in the shutdown phase, try...catch is useless.
Top 3 Fatal PHP Errors and How to Avoid Them
\n\n1. Calling a Non-Existent Method or Class
\n\nThe most common fatal error is Call to undefined method or Class not found. It occurs when you try to call a method that doesn't exist or use a class that hasn't been loaded.
How to avoid:
\n- \n
- Always use class autoloading (spl_autoload_register or Composer). \n
- Check if a method exists using
method_exists(). \n
if (method_exists($object, 'someMethod')) {\n $object->someMethod();\n} else {\n // handle it\n}\n\n2. Memory Exhaustion (Allowed memory size exhausted)
\n\nThe error Allowed memory size of X bytes exhausted is a fatal error that occurs when a script consumes more memory than allowed. It cannot be caught in the usual way.
How to avoid:
\n- \n
- Optimize database queries. \n
- Use generators (
yield) to process large arrays. \n - Increase the memory limit in
php.inior viaini_set('memory_limit', '256M'). \n
3. Execution Timeout (Maximum execution time exceeded)
\n\nAlthough this error is not always fatal, in some configurations it completely stops script execution.
\n\nHow to avoid:
\n- \n
- Use
set_time_limit(0)for long-running scripts. \n - Break heavy operations into smaller parts. \n
How to Catch a Fatal PHP Error: 3 Proven Methods
\n\nNow let's get to the main point. There are several techniques that allow you to catch E_ERROR and execute code before the script dies.