Fatal Error: Uncaught Error
An unhandled Error exception was thrown that crashed the script.
Explanation
Fatal error: Uncaught Error occurs when PHP encounters an error that it cannot recover from, and no try-catch block handles it. Since PHP 7, many fatal errors are now throwable Error objects. Common examples include calling undefined functions, calling methods on non-objects, type errors, and out-of-memory conditions. The error message includes the specific error type, the message, the file, and line number.
Common Causes
- Calling undefined function or method
- Type mismatch in strict mode
- Memory exhaustion
- Calling method on non-object
- Missing required file
Solution
Identify the specific error type from the error message to understand what went wrong. Add try-catch blocks around code that might throw errors. Use type hints and return type declarations to catch type-related errors early. Check that all referenced functions and classes exist. Verify PHP version compatibility of your code. Use IDE static analysis to catch potential errors before runtime.
Code Example
// Calling undefined function
undefinedFunction(); // Fatal: Call to undefined function
// Type error in strict mode
declare(strict_types=1);
function add(int $a, int $b): int {
return $a + $b;
}
add('one', 2); // Fatal: TypeError
// Fix: wrap in try-catch
try {
$result = riskyOperation();
} catch (\Error $e) {
Log::error('Error: ' . $e->getMessage());
return response()->json(['error' => 'Something went wrong'], 500);
}
Error Information
Language
php
Difficulty
Beginner
Views
192
Related Errors
PHP
Undefined Array Key
Accessing an array index or key that does not exist.
PHP
Allowed Memory Size Exhausted
A script tried to allocate more memory than the allowed memory limit.
PHP
Call to a Member Function on Null
The code calls a method on a variable that is null instead of an object.
Database
PDOException (Database Connection)
PHP failed to connect to or communicate with the database server.