3

I have a try/catch block where I catch all Throwable exceptions.

try {
    ...
} catch (Throwable $ex) {
    ...
}

How do I, at runtime, figure out what the exact class of the throw exception is? I want to add multiple catch blocks to handle different exceptions differently, but am unable to find out the types of exceptions that are thrown.

Rok Povsic
  • 4,626
  • 5
  • 37
  • 53

1 Answers1

1

Try to dump get_class($ex) inside your catch block. It will give you the class name of $ex.

After the class name is found, you can use catch with exact class exception.

david
  • 3,225
  • 9
  • 30
  • 43
  • `get_class($ex)` outputs the value `Error`. Is it possible to get subclass of Error here? Or would the subclass be shown here if the actual Error subclass was thrown, but in this case the 'parent' Error object is being thrown (not a subclass of it)? – Rok Povsic Jul 19 '18 at 06:29
  • do you use `echo` or `var_dump` to output the value? – david Jul 19 '18 at 06:30
  • `var_dump` outputs `string(5) "Error"`. – Rok Povsic Jul 19 '18 at 06:42