RuntimeException
PHPERRORNotableRuntime

Error that can only be found at runtime

Quick Answer

Extend RuntimeException for your own domain-specific exceptions; catch it as a catch-all for unexpected runtime failures.

What this means

Base SPL exception for runtime errors — conditions that could not have been detected at definition time. Extend it for domain-specific runtime failures.

Why it happens
  1. 1External service unavailable
  2. 2Unexpected state in a long-running process

Fix

Domain exception extending RuntimeException

Domain exception extending RuntimeException
class PaymentGatewayException extends \RuntimeException {}

if (!$response->isSuccess()) {
    throw new PaymentGatewayException(
        "Payment failed: " . $response->getError(),
        $response->getCode()
    );
}

Why this works

Extending RuntimeException keeps your exceptions catchable by catch(\RuntimeException) handlers.

Code examples
Catch as fallbackphp
try {
    $result = $service->call();
} catch (ServiceException $e) {
    // specific handler
} catch (\RuntimeException $e) {
    logger()->error($e->getMessage());
    throw $e;
}

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PHP errors