Question Difference beween if() and try /catch()

Hi

Please consider the two code snippets. Can someone pls tell me which one is better and why?


$q = "insert into...";
mysql_query($q);

if(mysql_affected_rows()>0){
   $id = mysql_insert_id();
}else{
   $id = false;
}

if( ! $id ){
  echo 'Something went wrong: ' .  mysql_error();
}

AND


$q = "insert into...";

try{
  mysql_query($q);
}catch(Exception $e){
  echo echo 'Something went wrong: ' .  $e->getMessage();
}

From my understanding, the first one actually tells you if there was a problem in running the query.

Many thanks in advance

The try/catch method will not work on old mysql functions. try/catch is for newer code that throws exceptions. Older functions and error handling is limited to if statements checking for false or other signs of an error. You use try/catch when working with exceptions and only then when you need to have something to do with it. If all you are going to do is kill the program on the first exception and display a message, DO NOT use a try/catch. An exception already kills the execution when not caught. Instead use “set_exception_handler” to handle displaying pretty messages for uncaught exceptions.

http://php.net/manual/en/language.exceptions.php

Just to clarify. If you are going to catch an exception and then within the catch you are just going to throw another exception. DO NOT USE try/catch. Let the exception bubble thought uncaught. For an example, DO NOT do this:


try {
  some_function_throwing_exception();
} catch ( Exception $e ) {
 throw new Exception( $e->getMessage() );
}

DO NOT DO THAT!

Thanks very much for the explanation.

Appreciate it.

Simply because i haven’t run into this yet (as I’ve learned never to do the above from other languages), but does throwing the exception in the way demonstrated above lose the stack trace like it does in other languages? The call to new Exception makes the exception show up as occurring in that catch block instead of inside some_function_throwing_exception() or worse a sub-function of some_function_throwing_exception()?

If you don’t know off the top of your head, I’ll plan on trying it later today when I have time :slight_smile:

You do lose the stack trace if you rethrow an exception, there is a $previous optional parameter in the Exception class you can use for chaining Exceptions. However it should only be used when the recovery from an exception fails.

[COLOR=#000000][FONT=verdana]public [/FONT][/COLOR][COLOR=#000000][FONT=verdana][B]Exception::__construct()[/B][/FONT][/COLOR][COLOR=#000000][FONT=verdana] ([ [/FONT][/COLOR][COLOR=#000000][FONT=verdana]string $message[I] = ""[/I][/FONT][/COLOR][COLOR=#000000][FONT=verdana] [, [/FONT][/COLOR][COLOR=#000000][FONT=verdana]int $code[I] = 0[/I][/FONT][/COLOR][COLOR=#000000][FONT=verdana] [, [/FONT][/COLOR][COLOR=#000000][FONT=verdana][Exception](http://www.php.net/manual/en/class.exception.php) $previous[I] = [B]NULL[/B][/I][/FONT][/COLOR][COLOR=#000000][FONT=verdana] ]]] )[/FONT][/COLOR]

Thanks, glad it follows suit with other languages implementation too :slight_smile:

Note that if you’re clever enough you can use exception try/catch routines instead of if/else sequences – but this is the sort of clever Wile E. Coyote is famous form. Clever != smart.

Still, I’ve seen try/catch abused into a replacement for normal conditionals. They aren’t meant for that - they are meant for the handling of exceptional conditions - like failures to connect to resources.