Can we use multiple try catch in php?
Update:As of PHP 7.1, this is available. Show The syntax is:
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 For PHP before 7.1:Despite what these other answers say, you can catch
Then:
As you can see here and here, even the
This means you could also have
which you need to handle differently than
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.
And then:
Using OOP when it comes to exceptions is very powerful. Using things like Another solution I would like to add is putting the exception handling functionality in its own method. You could have
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:
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. Home / Catch mutiple exception types with PHP PHP’s try..catch can be used to catch multiple exception types. If the try block could cause one of several different exceptions they can each be handled separately with their own catch
section. Example exceptionsHere’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 exceptionsIt’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. Exception SeriesThis is the six post in a weekly series of seven about PHP exceptions. Read the previous post "Extend PHP’s exception object", the last post in the series "Exiting from within a PHP exception" and use the links below to subscribe to my RSS feed, by email, or follow me on Twitter or Facebook to keep up to date with my daily postings. Can you have 2 tryYou cannot have multiple try blocks with a single catch block. Each try block must be followed by catch or finally. Still if you try to have single catch block for multiple try blocks a compile time error is generated.
How can you handle multiple exception in PHP?Rules for exceptions. Code may be surrounded in a try block, to help catch potential exceptions.. Each try block or "throw" must have at least one corresponding catch block.. Multiple catch blocks can be used to catch different classes of exceptions.. Exceptions can be thrown (or re-thrown) in a catch block within a try block.. Can we use tryThe 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.
|