Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一种情况,如果能够有一个在运行时确定异常类型的 catch 块会很好。它会像这样工作:
$someClassName = determineExceptionClass(); try { $attempt->something(); } catch ($someClassName $e) { echo 'Dynamic Exception'; } catch (Exception $e) { echo 'Default Exception'; }
这是可能吗?
据我所知,这不起作用。您可以使用如下控制语句来模仿该功能:
$someClass = 'SomeException'; try { $some->thing(); } catch (Exception $e) { switch (get_class($e)) { case $someClass: echo 'Dynamic exception.'; break; default: echo 'Normal exception.'; } }