Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

try/catch does not catch a missing require or include file

ErrorWarning: include(config.php): Failed to open stream: No such file or directory

Short answer

In PHP 8 a failed require throws an Error you can catch with catch (Throwable) or catch (Error). A failed include only raises a warning, so try/catch ignores it; convert warnings to ErrorException with set_error_handler. Before PHP 8.0 a failed require was an uncatchable E_COMPILE_ERROR.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the include in a tiny script with the exact path and see which of the two shapes you get: a warning with the script continuing, or a fatal stop. That decides which cause below applies.

    <?php
    include __DIR__ . '/missing.php';
    echo "still running\n";
    require __DIR__ . '/missing.php';
    echo "never reached\n";
  2. 2Match what you see, then jump to its fix

    1. 1an uncaught Error from require, with catch (Exception $e) in the coderequire throws an Error, and the catch type is too narrowFix
    2. 2Warning: include(...): Failed to open stream, and the script keeps runninginclude only raises a warning, so there is nothing to catchFix

What does the error look like?

  • Warning: include(config.php): Failed to open stream: No such file or directory
  • try/catch around include() does not catch anything
  • the script stops with a fatal error when a required file is missing

Cause 1 of 2

require throws an Error, and the catch type is too narrow

1Check

The manual says a failed require produces an Error exception from PHP 8.0, where earlier versions raised an E_COMPILE_ERROR. Error does not extend Exception, so catch (Exception) misses it.

php -v

2Fix

Catch Throwable, or Error. Use the same block for any other fatal error you want to survive.

try {
    require __DIR__ . '/config/local.php';
} catch (Throwable $e) {
    error_log($e->getMessage());
    // fall back to defaults here
}

3Verify

Rename the file and run the script. The message is logged and the script continues.

php test.php

Confirmed by php.net

Did this fix it?

Cause 2 of 2

include only raises a warning, so there is nothing to catch

1Check

include produces an E_WARNING when it fails and execution continues. A warning is not an exception, so try/catch around it does nothing.

grep -rn "include" src/ | head

2Fix

Turn warnings into ErrorException with set_error_handler, wrap the include in try/catch, and restore the previous handler afterwards. Return false for errors your error_reporting level excludes.

set_error_handler(function (int $no, string $str, string $file, int $line): bool {
    if (!(error_reporting() & $no)) {
        return false;
    }
    throw new ErrorException($str, 0, $no, $file, $line);
});
try {
    include __DIR__ . '/optional.php';
} catch (ErrorException $e) {
    error_log($e->getMessage());
} finally {
    restore_error_handler();
}

3Verify

Run it with the file missing: the exception is caught and the script goes on.

php test.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Use require for files the app cannot run without and let it stop loudly; use include only for optional files.
  • Do not put @ in front of include to hide a missing file; the error still happens and you lose the message.
  • Log the Throwable message with the path, so a deploy that forgot a file is found quickly.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.