Permalink
1 comment
on commit
sign in to comment.
Browse files
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
with
162 additions
and 23 deletions.
- +5 −0 Zend/tests/try/exceptions.inc
- +19 −0 Zend/tests/try/try_multicatch_001.phpt
- +21 −0 Zend/tests/try/try_multicatch_002.phpt
- +21 −0 Zend/tests/try/try_multicatch_003.phpt
- +21 −0 Zend/tests/try/try_multicatch_004.phpt
- +25 −0 Zend/tests/try/try_multicatch_005.phpt
- +6 −3 Zend/zend_ast.c
- +37 −18 Zend/zend_compile.c
- +7 −2 Zend/zend_language_parser.y
| @@ -0,0 +1,5 @@ | ||
| +<?php | ||
| + | ||
| +class Exception1 extends Exception {} | ||
| +class Exception2 extends Exception {} | ||
| +class Exception3 extends Exception {} |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |
| @@ -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 |
0aed2cccrash when :