PHP: Dynamic or Programmatic Catch Blocks
问题 I have a situation where it would be nice to be able to have a catch block where the type of the Exception is determined at run time. It would work something like this: $someClassName = determineExceptionClass(); try { $attempt->something(); } catch ($someClassName $e) { echo 'Dynamic Exception'; } catch (Exception $e) { echo 'Default Exception'; } Is this at all possible? 回答1: That doesn't work as far as I'm aware. You could mimic that functionality with a control statement like this: