PHP:动态或程序化捕获块 [英] PHP: Dynamic or Programmatic Catch Blocks
本文介绍了PHP:动态或程序化捕获块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种情况,能够在运行时确定异常类型的catch块。它可以这样工作:
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';
}
这是可以吗?
推荐答案
这是不行的,我知道。你可以用这样的控制语句来模仿那个功能:
That doesn't work as far as I'm aware. You could mimic that functionality with a control statement like this:
$someClass = 'SomeException';
try
{
$some->thing();
}
catch (Exception $e)
{
switch (get_class($e))
{
case $someClass:
echo 'Dynamic exception.';
break;
default:
echo 'Normal exception.';
}
}
这篇关于PHP:动态或程序化捕获块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文