I have a situation where it would be nice to be able to have a catch block where the type of the Exception is determined at run time. It would work something like this:
$someClassName = determineExceptionClass();
try {
$attempt->something();
} catch ($someClassName $e) {
echo 'Dynamic Exception';
} catch (Exception $e) {
echo 'Default Exception';
}
Is this at all possible?
Each catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, ExceptionType , declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name .
catch block to catch all exceptions throughout the application which are shown to user as, for example, error 500s and exception is logged with more information to database.
It always executes, regardless of whether an exception was thrown or caught. You can nest one or more try statements. If an inner try statement does not have a catch -block, the enclosing try statement's catch -block is used instead. You can also use the try statement to handle JavaScript exceptions.
That doesn't work as far as I'm aware. You could mimic that functionality with a control statement like this:
$someClass = 'SomeException';
try
{
$some->thing();
}
catch (Exception $e)
{
switch (get_class($e))
{
case $someClass:
echo 'Dynamic exception.';
break;
default:
echo 'Normal exception.';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With