hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

[ Feature Request ] try {} catch {} multiple-catch

Open lexidor opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe. One of the things I "miss" from the PHP days is support for multiple-catch. This was a nice to have and really helps when refactoring exceptions, since you need to catch OldException and NewException and treat them the same until OldException is no longer being thrown.

Describe the solution you'd like A clear and concise description of what you want to happen.

<?php
try {
  func();
} catch (\OutOfBoundsException | \OutOfRangeException $e) {
  // code that runs when either of these OOB or OOR is caught.
}

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Duplicating the // code that runs... in two catch blocks. This is sub-optimal for maintainability.

Nested try block where the inner try merely changes the exception type to (if effect) goto the other catch block.

try {
  try {
    func();
  } catch (\OutOfRangeException $e) {
    // Upgrade old exception to new exception.
    throw new OutOfBoundsException($e->getMessage(), $e->getCode(), $e);
  } 
} catch (\OutOfBoundsException $e) {
 // code that runs when either of these OOB or OOR is caught.
}

Additional context Add any other context or screenshots about the feature request here.

HHVM did not support this PHP 7.1 feature in hhvm 3.30, so there may very well be engine limitations for why this could not be implemented.

Having this in Hack before the introduction of (internal) union types in hhvm 4.14.0 would have been required the inference to infer the greatest common divisor of these types \Exception or unresolved of OOR and OOB. However, Hack now understands unions in inference and creating variables that hold intersections is not something we couldn't already do with $e = \random_int(0, 1) ? new A() : new B().

lexidor avatar Jun 03 '20 12:06 lexidor

The code sample your provided will fail in PHP, as that is not the intention of PHP multi-catch, PHPs multi-catch will catch only exceptions from the try block, but never from another catch block.

e.g: https://3v4l.org/rJAh5

azjezz avatar Dec 04 '20 22:12 azjezz