-3

Okay, so I'm new to error handling, and I've seen some examples but I havent seen an answer for this question. I'll use some real basic example code to show what I'm asking.

if(some condition){
    throw Exception()
  }

  //Some random code in between
  echo "Code between throw() and Catch()";

catch(Exception $e){
//handle the caught exception
}

So basically, my question is this - if the condition in the if() causes the exception to be thrown, will the random echo statement execute, or will it skip and go straight to the catch() of the exception?

Eric Diviney
  • 327
  • 2
  • 5
  • 16

3 Answers3

5

From the manual:

When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block

John Conde
  • 217,595
  • 99
  • 455
  • 496
4

The answer is NO,

EXAMPLE:

   <?php try{   
          $conn = new PDO('mysql:host=localhost; dbname=xxx', 'xxx', '');
      ?>  

...I am just an HTML text. ...

   <?php
       }catch(PDOException $e){
          echo  'ERROR: '.$e->getMessage();
       }    

This is because, once the try directive fails, it will quickly jump into catching that error, and display the error. So, you can't see/evaluate anything in-between.

samayo
  • 16,163
  • 12
  • 91
  • 106
0

once the throw identified and executed then control will be moved to catch block. all other statements will be skipped.

Rameshkrishnan S
  • 413
  • 1
  • 3
  • 8