Page 1 of 1

Handling Exit Statuses and Exception Levels

Posted: Thu Aug 31, 2006 4:15 am
by jamiel
If your script needs to exit with a different status based on the seriousness of the exception, is it cheating to use the Error Code from the Exception?

Code: Select all

// Warning
throw new Exception($message, 1, __CLASS__);
// Fatal
throw new Exception($message, 2, __CLASS__);
Example Usage:

Code: Select all

try {
   // Script
} catch (Exception $e) {
   // I will be doing more with this based on warning or fatal, but for now ...
   exit($e->getCode());
}
or should I stick to the following to avoid confusion...

Code: Select all

class Warning_Exception extends Exception { }
    class Fatal_Exception extends Exception { }

Posted: Thu Aug 31, 2006 4:23 am
by Jenk
Stick to the latter :)

Code: Select all

try {
    $object->somethingThatThrowsAnException();
} catch (OutOfBoundsException $e) {
    echo 'out of bounds!';
} catch (FileNotFoundException $e) {
    echo 'File not found!';
} catch (Exception $e) {
    echo 'Unknown exception!';
}