Let's say were using custom extensions of the Exception class to handle custom exceptions, like this for example:

$testObject = new testClass();

and a autoload like this:

function __autoload($class_name) {
    $file = $class_name.'.php';
    if (file_exists($file)) {
        include $file;  
    }else{
        throw new loadException("File $file is missing");
    }
    if(!class_exists($class_name,false)){
        throw new loadException("Class $class_name missing in $file");
    }
    return true;
}

try {
    $testObject = new testClass();
}catch(loadException $e){
    exit('<pre>'.$e.'</pre>');
}

the file testClass.php does not exist, so a loadException is called with the message: File testClass.php is missing. (and all the other details...line number etc)

all was fine, until i decided to hide all the errors and instead display a 404 page (or a 500 page...), so naturally i thought to add a loadErrorPage function.

class loadException {

...

    function loadErrorPage($code){
        $page = new pageClass();
        echo $page->showPage($code);
    }
}

...

try {
    $testObject = new testClass();
}catch(loadException $e){
    $e->loadErrorPage(500);
}

but this has the obvious problem that if the testClass.php AND pageClass.php files are missing, then a fatal error is shown instead of the preferred 404 page.

I'm confused :S How do I elegantly handle this exception within a exception handle?

Accepted Answer

If class pageClass does not exist and cannot be loaded by your autloader $page = new pageClass(); in your method loadErrorPage() will cause another exception. You'd have to catch this exception and then do something without that class.

function loadErrorPage($code){
  try {
    $page = new pageClass();
    echo $page->showPage($code);
  }
  catch(Exception $e) {
    // header(...500);
    echo 'fatal error: ', $code;
  }
}
Written by VolkerK
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki