PHP check thrown exception type

时间:2016-07-11 21:52:21

标签: php exception-handling try-catch

Of course in PHP you can catch all thrown exceptions with:

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

But is there a way to check the exception type of the thrown exception from inside the catch block?

3 个答案:

答案 0 :(得分:47)

You can use get_class:

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

答案 1 :(得分:11)

You can have multiple catch blocks to catch different Exception types. See below:

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.
}

You should know that once an Exception is caught by a catch statement, none of the following catch statements will be triggered, even if they match the Exception.

You can also use the get_class method to get the full class name of any object, including Exceptions.

答案 2 :(得分: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)   
}