Page 1 of 1

how you handle errors ?

Posted: Wed Mar 05, 2008 1:39 pm
by staar2
I am thinking to use exception in my application and i taught to make error handler in index.php file string set_exception_handler ( string exception_handler) setting some function handle exceptions which prints some output to user and sends report to admin. I use exception only in critical cases like when DB hasn't made connection or some necessary files are missing. So how you handle erros or exceptions ?

Re: how you handle errors ?

Posted: Wed Mar 05, 2008 10:20 pm
by Ambush Commander
It's highly context dependent, and often the client dictates what error handling policy should be. A safe approach is to politely inform the user that an error occured, and log the details. Emailing the admin can be a little risky, since errors tend to occur in clusters.

Re: how you handle errors ?

Posted: Wed Mar 05, 2008 10:50 pm
by alex.barylski
Ambush Commander wrote:Emailing the admin can be a little risky, since errors tend to occur in clusters
And if anyone finds out and doesn't like you -- they can deliberately invoke errors and flood your inbox.

Re: how you handle errors ?

Posted: Thu Mar 06, 2008 7:14 am
by staar2
yes that's true but have you made whole application by using only exceptions?

Re: how you handle errors ?

Posted: Mon Mar 10, 2008 2:56 pm
by anto91
staar2 wrote:yes that's true but have you made whole application by using only exceptions?
I have.

For the more "important" errors lets say system_exceptions

Code: Select all

 
<?php
class System_Exception extends Exception {
 
    const ERR_SQL_FAILURE = 1;
    const ERR_FILE_NOT_FOUND = 2;
    
    public function __construct($exception) {
        $this->code = $exception;
    }
 
}
 
 
try {
    if(file_exists('config.php')) {
        include('config.php');
    } else {
        throw new System_Exception(System_Exception::ERR_FILE_NOT_FOUND);
    }
    
    $name = 'foo';
    
    if($name != 'bar') {
        throw new Exception('Username was invalid!');
    }
    
} catch(System_Exception $e) {
    // Handle error here maybe needs to be logged.
} catch(Exception $e) {
    // Maybe use this function to display a error page?
    // To retrive the error message passed to the parameter
    $message = $e->getMessage();
}
?>