how you handle errors ?

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
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

how you handle errors ?

Post 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 ?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: how you handle errors ?

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: how you handle errors ?

Post 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.
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Re: how you handle errors ?

Post by staar2 »

yes that's true but have you made whole application by using only exceptions?
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: how you handle errors ?

Post 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();
}
?>
 
Post Reply