Rtrow php exception in higher level catch block

I am trying to pass an exception from a specific catch block to a more general catch block. However, it does not work. I get a 500 server error when I try to do the following. Is it possible?

I understand that there are easy workarounds, but isnโ€™t it okay to say: โ€œHey, I donโ€™t feel that I am dealing with this error, let a more general exception handler take care of this!โ€

try { //some soap stuff } catch (SoapFault $sf) { throw new Exception('Soap Fault'); } catch (Exception $e) { echo $e->getMessage(); } 
+7
source share
1 answer

Technically, this is what you are looking for:

 try { try { //some soap stuff } catch (SoapFault $sf) { throw new Exception('Soap Fault'); } } catch (Exception $e) { echo $e->getMessage(); } 

however, I agree that exceptions should not be used to control flow. A better way would be this:

 function show_error($message) { echo "Error: $message\n"; } try { //some soap stuff } catch (SoapFault $sf) { show_error('Soap Fault'); } catch (Exception $e) { show_error($e->getMessage()); } 
+7
source

All Articles