How to Catch a Fatal PHP Error (E_ERROR): Traps and Solutions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Catch a Fatal PHP Error (E_ERROR) and Keep Your Sanity

\n\n

Have 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.

\n\n

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\n

Why Doesn't try...catch Save You from E_ERROR?

\n\n

Let'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.

\n\n

Here is a simple example:

\n\n
try {\n    $obj = new NonExistentClass(); // This will trigger E_ERROR\n} catch (Throwable $e) {\n    echo 'Caught: ' . $e->getMessage();\n}
\n\n

In 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.

\n\n

Top 3 Fatal PHP Errors and How to Avoid Them

\n\n

1. Calling a Non-Existent Method or Class

\n\n

The 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.

\n\n

How to avoid:

\n
    \n
  • Always use class autoloading (spl_autoload_register or Composer).
  • \n
  • Check if a method exists using method_exists().
  • \n
\n\n
if (method_exists($object, 'someMethod')) {\n    $object->someMethod();\n} else {\n    // handle it\n}
\n\n

2. Memory Exhaustion (Allowed memory size exhausted)

\n\n

The 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.

\n\n

How to avoid:

\n
    \n
  • Optimize database queries.
  • \n
  • Use generators (yield) to process large arrays.
  • \n
  • Increase the memory limit in php.ini or via ini_set('memory_limit', '256M').
  • \n
\n\n

3. Execution Timeout (Maximum execution time exceeded)

\n\n

Although this error is not always fatal, in some configurations it completely stops script execution.

\n\n

How to avoid:

\n
    \n
  • Use set_time_limit(0) for long-running scripts.
  • \n
  • Break heavy operations into smaller parts.
  • \n
\n\n

How to Catch a Fatal PHP Error: 3 Proven Methods

\n\n

Now 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.

\n\n

Method 1. Using register_shutdown_function()

Blogs

Book Recommendations