【问题标题】:PHP check thrown exception typePHP 检查抛出的异常类型
【发布时间】:2016-11-13 23:34:48
【问题描述】:

当然,在 PHP 中,您可以通过以下方式捕获所有抛出的异常:

try{
    /* code with exceptions */
}catch(Exception $e) {
    /* Handling exceptions */
}

但是有没有办法从 catch 块中检查抛出的异常的异常类型?

【问题讨论】:

    标签: php exception-handling try-catch


    【解决方案1】:

    你可以使用get_class:

    try {
        throw new InvalidArgumentException("Non Sequitur!", 1);
    } catch (Exception $e) {
        echo get_class($e);
    }
    

    【讨论】:

    • 正是我想要的,我会再次投票:)
    【解决方案2】:

    您可以有多个catch 块来捕获不同的异常类型。 见下文:

    try {
        /* code with exceptions */
    } catch (MyFirstCustomException $e) {
        // We know it is a MyFirstCustomException
    } catch (MySecondCustomException $e) {
        // We know it is a MySecondCustomException
    } catch (Exception $e) {
        // If it is neither of the above, we can catch all remaining exceptions.
    }
    

    您应该知道,一旦catch 语句捕获到异常,即使它们与异常匹配,也不会触发以下catch 语句。

    您还可以使用get_class 方法获取任何对象的完整类名,包括异常。

    【讨论】:

    • 我认为这是正确的答案。测试类类型意味着您可能知道您要查找的类类型是什么,因此可以创建适当的 catch 语句。
    【解决方案3】:

    我认为使用instanceof 是一个更好的解决方案,因为它为您提供的信息不仅仅是您从get_class 获得的类名。

    class db_exception extends Exception {}
    class db_handled_exception extends db_exception {}
    class db_message_exception extends db_exception {}
    
    function exception_type($ex){
        echo '<pre>';
        echo 'Type: [' . get_class($ex) . "]\n";
        echo "Instance of db_message_exception? " . var_export($ex instanceof db_message_exception,true) . "\n";
        echo "Instance of db_handled_exception? " . var_export($ex instanceof db_handled_exception,true) . "\n";
        echo "Instance of db_exception?         " . var_export($ex instanceof db_exception,true) . "\n";
        echo "Instance of Exception?            " . var_export($ex instanceof Exception,true) . "\n";
        echo '</pre>';
    }
    
    exception_type(new db_handled_exception());
    exception_type(new db_message_exception());
    exception_type(new db_exception());
    exception_type(new exception());
    

    结果如下

    Type: [db_handled_exception]
    Instance of db_message_exception? false
    Instance of db_handled_exception? true
    Instance of db_exception?         true
    Instance of Exception?            true
    
    Type: [db_message_exception]
    Instance of db_message_exception? true
    Instance of db_handled_exception? false
    Instance of db_exception?         true
    Instance of Exception?            true
    
    Type: [db_exception]
    Instance of db_message_exception? false
    Instance of db_handled_exception? false
    Instance of db_exception?         true
    Instance of Exception?            true
    
    Type: [Exception]
    Instance of db_message_exception? false
    Instance of db_handled_exception? false
    Instance of db_exception?         false
    Instance of Exception?            true
    

    您可能希望对异常进行分类并执行常见操作。

    考虑到上面的例子,您可能只想显示db_message_exceptiondb_handled_exception 类型的异常;在这种情况下,由于它们都是从 db_exception 继承的,因此您可以简单地说:

    if ($ex instanceof db_exception){
       // show error message
    }
    

    您可能还想包装您的异常以避免将太多信息溢出到客户端屏幕:

    if (!($ex instanceof db_exception)){
        throw new db_handled_exception('an unhandled exception occured', -1, $ex)   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      相关资源
      最近更新 更多