Q:

Is there a way in PHP to leave a function upon error

To minimize page loading time, we are caching our PHP generated HTML as an HTML file. If a "cache file" exists we use it. Otherwise we run the PHP scripts to generate the page. 
To reduce the page loading speed even more, we would like to remove the "If file exists" line (which takes time) and simply let PHP generate an error if the file does not exist.  We would then "catch" the error and let PHP generate the page.
The question is the following: Is there a simple way to implement "On error, leave function" in PHP.
This is the code we have:
function UseCachedPage()
{
$CacheFile='mypage.html';
if(!file_exists($CacheFile)) return; //Would like to skip this to save time
include($CacheFile); 
//Magic "on error, leave function" needed here.
exit; //Cache included, so no need to run the php.
}

Any help would be appreciated.

A:

According to the docs:

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

So what this means is if you want to "catch" it, you have to use require instead. Now, you can only catch this fatal error as of PHP 7, which is important to know.
function UseCachedPage() {
    $CacheFile = "mypage.html";
    try {
        require($CacheFile);
        exit;
    } catch (Error $e) {
        // Call something to build your file here
    }
}

