Martin
Martin

Reputation: 22760

PHP how be sure to catch all exceptions

Suppose I have some code that uses try{...} catch() { ... } blocks. Within the tried code, there are various non-trivial third party objects (or maybe other data).

Often these third party objects will throw their own third party exceptions; how can I be sure to catch these without knowing what these exceptions are actually named?

With various third party objects and methods in various parts of the code, is there a relatively quick way of catching all exceptions thrown for further investigation?

Detailed Usage Example (Stripe has numerous Exception cases)

$someObject = new \someNamespace\someClass();
try {
    $x = $someObject->someFunction($someData); 
    $y = $stripeObject->StripeProcessing($x);
}
catch(\Stripe\Error\Card $ex) {
        // Since it's a decline, \Stripe\Error\Card will be caught
        error_log("Stripe Card Error: ".$ex->message);
        $message = "There was a card error: ".$err['message'];
    } 
catch (\Stripe\Error\RateLimit   | 
    \Stripe\Error\InvalidRequest | 
    \Stripe\Error\Authentication |   
    \Stripe\Error\ApiConnection  |  
    \Stripe\Error\Base $ex) {
        $message = "There was a Stripe error: ".$ex->getMessage();
        error_log("Stripe Other Error: ".print_r($ex,true));
    } 
catch( \someNamespace\GiggityException |
    \someNamespace\GiggityNewMexicoException){
    error_log("Family Guy! ".print_r($ex,true));
    }
catch (Exception $ex){
    error_log("Elvis has died! ".print_r($ex,true));
}

Could result in:

PHP Fatal error: Uncaught SomeDistributor\SomeClass\Exception: connection() failed. ...etc...etc...

So above has a bunch of Exceptions to catch, from two namespaces, as well as the general Exception at the end; While the code reactions to all of these are very similar; I still seem to need to know the name of each and every Exception class thrown and have these manually coded in;

So, is there a way of catching ALL possible exceptions in one place?

(Before then differentiating and branching how to handle specific ones, as needed)

I'm sure the solution to this is vey simple but I can't find liteature on this (d'oh; I have since found literature).

Upvotes: 3

Views: 4456

Answers (2)

user24973147
user24973147

Reputation: 11

As a note for the accepted answer:

For TypeErrors (created via type hinting) the catch ( \Exception $ex ) will not catch the error, but crash the program with a Fatal Error.

You can use a moe generic catch for all Throwables:

try{
    //code
} catch (Throwable $e) {
    // delegate exception up
    throw $e;
} 

See also:

https://stackoverflow.com/a/69301914

Upvotes: 1

Martin
Martin

Reputation: 22760

This website states:

The simplest way to catch exceptions is through the use of a generic try-catch block. Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front:

try {
  // ...
  } catch ( \Exception $ex ) {
    // ...
  }

Upvotes: 5

Related Questions