tags:

views:

165

answers:

3

Is it considered bad practice to let die() live in a production enviroment? Just happened to read this article http://www.phpfreaks.com/blog/or-die-must-die where the author fies upon people who use such a thing in a production enviroment. So shouldn't I code this way:

$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
    die ("Could not connect to the database.");
}

How do you code?

+2  A: 

die() is a very rough statement... Useful (quite) in developer stage, i found it wrong in production stage.

You should analyze, monitor and log fatal errors, and displaying adequate messages like "It's impossible to connect to the server, please try in few minutes or write to [email protected] to notify the problem"!

Enrico Carlesso
+1  A: 

You don't die everytime you make a mistake, do you. Why should your application do?

the correct way is to intercept errors and process them in a context-dependent fashion, for example

try {
    application goes here

    $conn = mysql_connect(...)
    if(!$conn)
        throw ....
    ....
} catch(Exception $err) {
     if(PRODUCTION) {
        log error
        say something nice
     }
     if(DEBUG) {
         var_dump($err);
     }
}
stereofrog
A subjective question. Why do you prefer to raise and catch an exception instead of handling in the `if(!$conn)` condition?
Enrico Carlesso
Enrico, because exceptions are better suited to breaking out of the standard flow of the program and passing control to error handling code. It is also a more robust way to 'crash early': if instead you rely on if/else conditions everywhere, you have to remember every time to call the correct functions to terminate execution.
Ben James
+1  A: 

Well in a productive enivornment, you should never expose any errors/information about the system to the outside world.

Important is, to log all errors. If you are talking about a website, I would send an HTTP status 500 as response.

Felix Kling