Handle Multiple Exceptions in a catch Block In Java SE 7 and later, we can now catch more than one type of exception in a single catch block. Each exception type that can be handled by the catch block is separated using a vertical bar or pipe | .
Java Catch Multiple Exceptions A try block can be followed by one or more catch blocks. Each catch block must contain a different exception handler. So, if you have to perform different tasks at the occurrence of different exceptions, use java multi-catch block.
Starting from Java 7.0, it is possible for a single catch block to catch multiple exceptions by separating each with | (pipe symbol) in the catch block. Catching multiple exceptions in a single catch block reduces code duplication and increases efficiency.
CPP. 2) There is a special catch block called the 'catch all' block, written as catch(…), that can be used to catch all types of exceptions. For example, in the following program, an int is thrown as an exception, but there is no catch block for int, so the catch(…) block will be executed.
As of PHP 7.1, this is available.
The syntax is:
try
{
// Some code...
}
catch(AError | BError $e)
{
// Handle exceptions
}
catch(Exception $e)
{
// Handle the general case
}
Docs: https://www.php.net/manual/en/language.exceptions.php#example-294
RFC: https://wiki.php.net/rfc/multiple-catch
Commit: https://github.com/php/php-src/commit/0aed2cc2a440e7be17552cc669d71fdd24d1204a
Despite what these other answers say, you can catch AError
and BError
in the same block (it is somewhat easier if you are the one defining the exceptions). Even given that there are exceptions you want to "fall through", you should still be able to define a hierarchy to match your needs.
abstract class MyExceptions extends Exception {}
abstract class LetterError extends MyExceptions {}
class AError extends LetterError {}
class BError extends LetterError {}
Then:
catch(LetterError $e){
//voodoo
}
As you can see here and here, even the SPL
default exceptions have a hierarchy you can leverage. Additionally, as stated in the PHP Manual:
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block.
This means you could also have
class CError extends LetterError {}
which you need to handle differently than AError
or BError
, so your catch statement would look like this:
catch(CError $e){
//voodoo
}
catch(LetterError $e){
//voodoo
}
If you had the case where there were twenty or more exceptions that legitimately belonged under the same superclass, and you needed to handle five (or whatever large-ish group) of them one way and the rest the other, you can STILL do this.
interface Group1 {}
class AError extends LetterError implements Group1 {}
class BError extends LetterError implements Group1 {}
And then:
catch (Group1 $e) {}
Using OOP when it comes to exceptions is very powerful. Using things like get_class
or instanceof
are hacks, and should be avoided if possible.
Another solution I would like to add is putting the exception handling functionality in its own method.
You could have
function handleExceptionMethod1(Exception $e)
{
//voodoo
}
function handleExceptionMethod2(Exception $e)
{
//voodoo
}
Assuming there is absolutely no way you can control exception class hierarchies or interfaces (and there almost always will be a way), you can do the following:
try
{
stuff()
}
catch(ExceptionA $e)
{
$this->handleExceptionMethod1($e);
}
catch(ExceptionB $e)
{
$this->handleExceptionMethod1($e);
}
catch(ExceptionC $e)
{
$this->handleExceptionMethod1($e);
}
catch(Exception $e)
{
$this->handleExceptionMethod2($e);
}
In this way, you are still have a only single code location you have to modify if your exception handling mechanism needs to change, and you are working within the general constructs of OOP.
In PHP >= 7.1 this is possible. See this answer.
If you can modify the exceptions, use this answer.
If you can't, you could try catching all with Exception
and then check which exception was thrown with instanceof
.
try
{
/* something */
}
catch( Exception $e )
{
if ($e instanceof AError OR $e instanceof BError) {
// It's either an A or B exception.
} else {
// Keep throwing it.
throw $e;
}
}
But it would probably be better to use multiple catch blocks as described in aforementioned answer.
try
{
/* something */
}
catch( AError $e )
{
handler1( $e );
}
catch ( BError $b )
{
handler2( $e );
}
Coming in PHP 7.1 is the ability to catch multiple types.
So that this:
<?php
try {
/* ... */
} catch (FirstException $ex) {
$this->manageException($ex);
} catch (SecondException $ex) {
$this->manageException($ex);
}
?>
and
<?php
try {
} catch (FirstException | SecondException $ex) {
$this->manageException($ex);
}
?>
are functionally equivalent.
As of PHP 7.1,
catch( AError | BError $e )
{
handler1( $e )
}
interestingly, you can also:
catch( AError | BError $e )
{
handler1( $e )
} catch (CError $e){
handler2($e);
} catch(Exception $e){
handler3($e);
}
and in earlier versions of PHP:
catch(Exception $ex){
if($ex instanceof AError || $ex instanceof BError){
//handle AError and BError
} elseif($ex instanceof CError){
//handle CError
} else {
throw $ex; // an unknown exception occurred, throw it further
}
}
This article covers the question electrictoolbox.com/php-catch-multiple-exception-types. Content of the post copied directly from the article:
Example exceptions
Here's some example exceptions that have been defined for the purposes of this example:
class FooException extends Exception
{
public function __construct($message = null, $code = 0)
{
// do something
}
}
class BarException extends Exception
{
public function __construct($message = null, $code = 0)
{
// do something
}
}
class BazException extends Exception
{
public function __construct($message = null, $code = 0)
{
// do something
}
}
Handling multiple exceptions
It's very simple - there can be a catch block for each exception type that can be thrown:
try
{
// some code that might trigger a Foo/Bar/Baz/Exception
}
catch(FooException $e)
{
// we caught a foo exception
}
catch(BarException $e)
{
// we caught a bar exception
}
catch(BazException $e)
{
// we caught a baz exception
}
catch(Exception $e)
{
// we caught a normal exception
// or an exception that wasn't handled by any of the above
}
If an exception is thrown that is not handled by any of the other catch statements it will be handled by the catch(Exception $e) block. It does not necessarily have to be the last one.
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