Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

How do you handle errors and exceptions in PHP 8, and what changed from earlier versions?

PHP historically had two parallel systems — traditional errors and exceptions — and PHP 7 unified them. Most fatal errors now throw Error, and both Error and Exception implement Throwable.

try {
    $result = $service->process($input);
} catch (ValidationException $e) {
    return $this->badRequest($e->getMessage());
} catch (Throwable $e) {
    $this->logger->error($e->getMessage(), ['exception' => $e]);
    return $this->serverError();
} finally {
    $this->cleanup();
}

What matters in practice:

  • Catch Throwable, not Exception, when you want a genuine catch-all — TypeError and DivisionByZeroError are Errors and will slip past Exception.
  • Define your own exception types. PaymentDeclinedException lets callers handle one failure differently from another; throwing a generic Exception forces string matching on messages.
  • Set an exception handler and an error handler at the application's entry point so nothing escapes unlogged, and convert warnings into ErrorException so they cannot be ignored.
  • Never show a stack trace to a user. display_errors off in production, log_errors on.

Note: PHP 8 also made many previously-silent warnings into errors — accessing an undefined array key, for example — which is why an upgrade surfaces bugs that were always there.

All Php interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as