Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP check thrown exception type

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?

like image 627
Jeffrey Cordero Avatar asked Jul 11 '16 21:07

Jeffrey Cordero


People also ask

How can I get exception type in PHP?

You can also use the get_class method to get the full class name of any object, including Exceptions. This in my opinion is the correct answer. to test the class type means you probably know what class type you are looking for is and therefore can create the appropriate catch statements.

Does PHP have try catch?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

What is PHP exception class?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.

How can I get fatal error in PHP?

You can "catch" these "fatal" errors by using set_error_handler() and checking for E_RECOVERABLE_ERROR. I find it useful to throw an Exception when this error is caught, then you can use try/catch.


3 Answers

You can use get_class:

try {
    throw new InvalidArgumentException("Non Sequitur!", 1);
} catch (Exception $e) {
    echo get_class($e);
}
like image 185
Don't Panic Avatar answered Oct 11 '22 20:10

Don't Panic


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.

like image 38
Tom Wright Avatar answered Oct 11 '22 20:10

Tom Wright


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)   
}
like image 38
AaA Avatar answered Oct 11 '22 20:10

AaA