How to efficiently use try…catch blocks in PHP

前端 未结 8 1418
自闭症患者
自闭症患者 2020-12-02 05:31

I have been using try..catch blocks in my PHP code, but I\'m not sure if I\'ve been using them correctly.

For example, some of my code looks like:

 t         


        
相关标签:
8条回答
  • 2020-12-02 05:55

    There is no any problem to write multiple lines of execution withing a single try catch block like below

    try{
    install_engine();
    install_break();
    }
    catch(Exception $e){
    show_exception($e->getMessage());
    }
    

    The moment any execption occure either in install_engine or install_break function the control will be passed to catch function. One more recommendation is to eat your exception properly. Which means instead of writing die('Message') it is always advisable to have exception process properly. You may think of using die() function in error handling but not in exception handling.

    When you should use multiple try catch block You can think about multiple try catch block if you want the different code block exception to display different type of exception or you are trying to throw any exception from your catch block like below:

    try{
        install_engine();
        install_break();
        }
        catch(Exception $e){
        show_exception($e->getMessage());
        }
    try{
    install_body();
    paint_body();
    install_interiour();
    }
    catch(Exception $e){
    throw new exception('Body Makeover faield')
    }
    
    0 讨论(0)
  • 2020-12-02 05:58

    in a single try catch block you can do all the thing the best practice is to catch the error in different catch block if you want them to show with their own message for particular errors.

    0 讨论(0)
  • 2020-12-02 06:05

    When an exception is thrown, execution is immediately halted and continues at the catch{} block. This means that, if you place the database calls in the same try{} block and $tableAresults = $dbHandler->doSomethingWithTableA(); throws an exception, $tableBresults = $dbHandler->doSomethingElseWithTableB(); will not occur. With your second option, $tableBresults = $dbHandler->doSomethingElseWithTableB(); will still occur since it is after the catch{} block, when execution has resumed.

    There is no ideal option for every situation; if you want the second operation to continue regardless, then you must use two blocks. If it is acceptable (or desirable) to have the second operation not occur, then you should use only one.

    0 讨论(0)
  • 2020-12-02 06:10

    For posterity sake,the answer maybe too late.You should check for the return value of the variable and throw an exception. In that case you are assured that the program will jump from where the exception is being raised to the catch block. Find below.

    try{
       $tableAresults = $dbHandler->doSomethingWithTableA();
       if (!tableAresults) 
         throw new Exception('Problem with tableAresults');
    
      $tableBresults = $dbHandler->doSomethingElseWithTableB();
       if (!tableBresults) 
         throw new Exception('Problem with tableBresults');
    } catch (Exception $e) {
        echo $e->getMessage();
    
    }
    
    0 讨论(0)
  • 2020-12-02 06:11

    It's more readable a single try catch block. If its important identify a kind of error I recommend customize your Exceptions.

    try {
      $tableAresults = $dbHandler->doSomethingWithTableA();
      $tableBresults = $dbHandler->doSomethingElseWithTableB();
    } catch (TableAException $e){
      throw $e;
    } catch (Exception $e) {
      throw $e;
    }
    
    0 讨论(0)
  • 2020-12-02 06:17

    There is no reason against using a single block for multiple operations, since any thrown exception will prevent the execution of further operations after the failed one. At least as long as you can conclude which operation failed from the exception caught. That is as long as it is fine if some operations are not processed.

    However I'd say that returning the exception makes only limited sense. A return value of a function should be the expected result of some action, not the exception. If you need to react on the exception in the calling scope then either do not catch the exception here inside your function, but in the calling scope or re-throw the exception for later processing after having done some debug logging and the like.

    0 讨论(0)
提交回复
热议问题