SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    737
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question 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?


    Code:
    $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

    Code:
    $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

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    try {
      some_function_throwing_exception();
    } catch ( Exception $e ) {
     throw new Exception( $e->getMessage() );
    }
    DO NOT DO THAT!
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    737
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much for the explanation.

    Appreciate it.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    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:

    Code:
    try {
      some_function_throwing_exception();
    } catch ( Exception $e ) {
     throw new Exception( $e->getMessage() );
    }
    DO NOT DO THAT!
    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

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    public Exception::__construct() ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    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.

    Code:
    public Exception::__construct() ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
    Thanks, glad it follows suit with other languages implementation too

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,026
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •