Can I create an exception without throwing it?

I use the SaaS service and the exception logging service called Rollbar. In my code, I have a static Rollbar object that I can use to report exceptions from the service.

For instance:

 try { ... throw new SomeException(); ... } catch (SomeException $e) { Rollbar::report_exception($e); } 

My question is: can I instantiate an exception without throwing it, as if it were any other normal object, and are there any reservations?

I would like to do the following:

 if($api_response_ok) { // Do some stuff ... } else { Rollbar::report_exception(new ApiException($api_error_msg)); } // Script execution continues... 
+4
source share
2 answers

Yes, the exception is like any other object.

+7
source

Exceptions are only objects that extend the exception class. They will break script execution only when they are thrown.

 $exception = new Exception('Die'); //Does not exit here throw $exception; //Will exit here 
+1
source

Source: https://habr.com/ru/post/1490871/


All Articles