-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow catching multiple exception types in a single catch statement
This commit add the possibility to catch multiple exception types in a single catch statement to avoid code duplication. try { // Some code... } catch (ExceptionType1 | ExceptionType2 $e) { // Code to handle the exception } catch (\Exception $e) { // ... }
- Loading branch information
Showing
9 changed files
with
162 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
class Exception1 extends Exception {} | ||
class Exception2 extends Exception {} | ||
class Exception3 extends Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
Parsing test | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/exceptions.inc'; | ||
|
||
try { | ||
echo 'TRY' . PHP_EOL; | ||
} catch(Exception1 | Exception2 $e) { | ||
echo 'Exception'; | ||
} finally { | ||
echo 'FINALLY' . PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
TRY | ||
FINALLY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Catch first exception in the multicatch | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/exceptions.inc'; | ||
|
||
try { | ||
echo 'TRY' . PHP_EOL; | ||
throw new Exception1; | ||
} catch(Exception1 | Exception2 | Exception3 $e) { | ||
echo get_class($e) . PHP_EOL; | ||
} finally { | ||
echo 'FINALLY' . PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
TRY | ||
Exception1 | ||
FINALLY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Catch second exception in the multicatch | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/exceptions.inc'; | ||
|
||
try { | ||
echo 'TRY' . PHP_EOL; | ||
throw new Exception2; | ||
} catch(Exception1 | Exception2 | Exception3 $e) { | ||
echo get_class($e) . PHP_EOL; | ||
} finally { | ||
echo 'FINALLY' . PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
TRY | ||
Exception2 | ||
FINALLY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Catch last exception in the multicatch | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/exceptions.inc'; | ||
|
||
try { | ||
echo 'TRY' . PHP_EOL; | ||
throw new Exception3; | ||
} catch(Exception1 | Exception2 | Exception3 $e) { | ||
echo get_class($e) . PHP_EOL; | ||
} finally { | ||
echo 'FINALLY' . PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
TRY | ||
Exception3 | ||
FINALLY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
Catch exception in the nested multicatch | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/exceptions.inc'; | ||
|
||
try { | ||
try { | ||
echo 'TRY' . PHP_EOL; | ||
throw new Exception3; | ||
} catch (Exception1 | Exception3 $e) { | ||
echo get_class($e) . PHP_EOL; | ||
} | ||
} catch(Exception2 | Exception3 $e) { | ||
echo 'Should never be executed'; | ||
} finally { | ||
echo 'FINALLY' . PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
TRY | ||
Exception3 | ||
FINALLY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0aed2cc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crash when :