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
17
Related Errors
Laravel
MalformedPayloadException
The request payload or session data is corrupted or invalid.
PHP
Trying to Access Array Offset on Value of Type Null
The code tries to use array access on a variable that is null instead of an array.
PHP
Cannot Redeclare Function
A function with the same name has already been defined in the current scope.
PHP
Undefined Variable
The code references a variable that has not been declared or assigned a value.