php - Use error outside catch block -


let's work example this:

try {     if( $var == false ) {         throw new exception('fail');     } } catch (exception $e) {     $error = $e->getmessage(); } 

is there way can use $error outside catch block? if try print outside,it shows nothing , i'm sure i'm doing wrong. apreciated.

edit: exact situation of code click

i need use $error , in piece of code , outside of class

actually returning error message, if remove return false statement, , try print outside loop print.

you can in 2 way:

option1:

try {   if( $var == false ){     throw new exception('fail');   } } catch (exception $e){   $error = $e->getmessage(); //  return false; } 

print_r($error);

option 2:

try {   if( $var == false ){     throw new exception('fail');   } } catch (exception $e){   return $error = $e->getmessage(); } 

Comments