mac*_*osh 6 php exception dynamic try-catch
我有一种情况,能够有一个catch块,在运行时确定Exception的类型会很好.它会像这样工作:
$someClassName = determineExceptionClass();
try {
$attempt->something();
} catch ($someClassName $e) {
echo 'Dynamic Exception';
} catch (Exception $e) {
echo 'Default Exception';
}
Run Code Online (Sandbox Code Playgroud)
这是可能吗?
就我所知,这不起作用.您可以使用如下控制语句模仿该功能:
$someClass = 'SomeException';
try
{
$some->thing();
}
catch (Exception $e)
{
switch (get_class($e))
{
case $someClass:
echo 'Dynamic exception.';
break;
default:
echo 'Normal exception.';
}
}
Run Code Online (Sandbox Code Playgroud)