PHP check thrown exception type

前端 未结 3 2060
半阙折子戏
半阙折子戏 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: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.

提交回复
热议问题