dimanche 30 août 2015

Throw exceptions with self-defined constants: best practice

I want to throw exceptions with self-defined constants so that I will be able to decide if to quit the program/retry/log the error and so on. Is there some best practice guide? Do I have to mind other library codes (they simply might have the same code number). Consider this:

<?php
class Foo {
    const FATALERROR = 1;
    const NOTFATAL = 2;

    function bar() {
        try {
            // some code
            throw Exception("This is a fatal error.", self::FATALERROR);
        } catch(Exception $e) {
            if ($e->getCode() === self::FATALERROR)
                die($e->getMessage());
            else
                // do some other stuff here
        }
    }
}
?>

Aucun commentaire:

Enregistrer un commentaire