phanalist

E0002: Empty Catch Block

In plain English: A catch block that is left completely empty without handling or logging the caught exception.

Why it matters: Catching an exception and doing nothing with it (“swallowing” the error) hides bugs, failed database queries, or invalid inputs. When something goes wrong, your application will fail silently or behave unpredictably without leaving any trace in your logs.

❌ The Bad Example

try {
    $this->saveUser($data);
} catch (Exception $e) {
    // Empty catch block! Error is swallowed silently.
}

✅ The Good Example

try {
    $this->saveUser($data);
} catch (DatabaseException $e) {
    // Log the error and rethrow or handle it appropriately
    Log::error("Failed to save user: " . $e->getMessage());
    throw new UserSaveFailedException("Could not save user.", 0, $e);
}

What the rule checks

When it does not report

How to fix

Configuration

E0002 has no configuration options and is enabled by default.

Junior Engineer’s Rule of Thumb:

Never leave a catch block empty. If an exception happens, at minimum log it or handle it so bugs don’t fail silently!