PHP: Dynamic or Programmatic Catch Blocks

前端 未结 1 1915
臣服心动
臣服心动 2020-12-11 05:14

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:

相关标签:
1条回答
  • 2020-12-11 05:26

    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.';
        }
    }
    
    0 讨论(0)
提交回复
热议问题