Handling Exit Statuses and Exception Levels

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Handling Exit Statuses and Exception Levels

Post 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 { }
Last edited by jamiel on Thu Aug 31, 2006 4:24 am, edited 2 times in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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!';
}
Post Reply