PHP检查抛出的异常类型

Jef*_*ero 20 php exception-handling try-catch

当然在PHP中,您可以捕获所有抛出的异常:

try{
    /* code with exceptions */
}catch(Exception $e) {
    /* Handling exceptions */
}
Run Code Online (Sandbox Code Playgroud)

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

Don*_*nic 53

你可以使用get_class:

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


Tom*_*ght 13

您可以使用多个catch块来捕获不同的Exception类型.见下文:

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.
}
Run Code Online (Sandbox Code Playgroud)

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

您还可以使用该get_class方法获取任何对象的完整类名,包括Exceptions.


AaA*_*AaA 6

我认为使用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());
Run Code Online (Sandbox Code Playgroud)

这将导致如下

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
Run Code Online (Sandbox Code Playgroud)

在某些情况下,您可能希望对异常进行分类并执行常见操作。

考虑上面的示例,您可能只想显示类型为db_message_exceptionand 的异常db_handled_exception;在这种情况下,由于它们都是从 db_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
Run Code Online (Sandbox Code Playgroud)

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

if ($ex instanceof db_exception){
   // show error message
}
Run Code Online (Sandbox Code Playgroud)