PHP check thrown exception type

前端 未结 3 2041
半阙折子戏
半阙折子戏 2021-01-01 09:58

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

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


        
相关标签:
3条回答
  • 2021-01-01 10:30

    You can use get_class:

    try {
        throw new InvalidArgumentException("Non Sequitur!", 1);
    } catch (Exception $e) {
        echo get_class($e);
    }
    
    0 讨论(0)
  • 2021-01-01 10:46

    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.

    0 讨论(0)
  • 2021-01-01 10:46

    I think using instanceof is a better solution, since it gives you more information than just class name which you get from 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());
    

    Which will result as following

    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
    

    There might be cases that you want to categorize exceptions and do common actions.

    Considering above example, you might want to only show exceptions that are of type db_message_exception and db_handled_exception; in this case since both of them are inherited from db_exception, you can simply say something like:

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

    You might also want to wrap your exceptions to avoid spilling too much information to client screen:

    if (!($ex instanceof db_exception)){
        throw new db_handled_exception('an unhandled exception occured', -1, $ex)   
    }
    
    0 讨论(0)
提交回复
热议问题