# Optional catch binding

- Thread: https://externals.io/message/104491
- Original archive: https://news-web.php.net/php.internals/104491
- Started by Roland Franssen on 2019-02-21T15:27:40+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #104491 — Roland Franssen — 2019-02-21T15:27:40+00:00

URL: https://externals.io/message/104491#104491

Hi Internals,

I'd like to propose an idea to allow catching exceptions by type only, or
globally, by any type.

In case the "$e" variable or the exception type is redundant, it doesn't
need to be specified:

try {
    x();
} catch (\Throwable) {
    // ...
}

try {
    x();
} catch {
    // ...
}

Example in the wild that could benefit from this:
https://github.com/symfony/symfony/blob/5a3e8942032c1a49496fe8db61828707b45442f9/src/Symfony/Component/Config/Definition/ArrayNode.php#L247
($e
variable is redundant).

Allthough global exception catching might not be a good practice per se, I
don't see a real reason to forbid it. However, these are 2 independent
features that can be voted for I guess.

For now, I'm aiming to get rid of "unused variable $e".

Thoughts?

See also
-
https://nitayneeman.com/posts/a-taste-from-ecmascript-2019/#optional-catch-binding
- https://en.cppreference.com/w/cpp/language/try_catch

Cheers,
Roland Franssen

PS: 3rd attempt, sorry if im spamming somehow

---

## #104495 — Stanislav Malyshev — 2019-02-21T19:35:24+00:00

URL: https://externals.io/message/104491#104495

Hi!

> Allthough global exception catching might not be a good practice per se, I
> don't see a real reason to forbid it. However, these are 2 independent
> features that can be voted for I guess.

It's not forbidden now. But there's no reason to encourage it and
develop a dedicated syntax for it. Current syntax works just fine. Yes,
it produces a warning - and it should, since what is being done is what
usually should not be done - ignoring exceptions.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #104497 — Roland Franssen — 2019-02-21T20:00:41+00:00

URL: https://externals.io/message/104491#104497

> it produces a warning - and it should, since what is being done is what
usually should not be done

I didn't want to turn this into an "error handling best practices"
discussion. Exceptions might not be recommended to use for flow-control,
yet this is possible today and really depends on the exception type itself
and it's fallback handling:

try {
  $o = $container->get('id');
} catch (ServiceNotFoundException) {
  $o = $container->get('other_id');
}

Leaving out the $e variable makes the lack of error handler more explicit.

Roland,

Op do 21 feb. 2019 om 20:35 schreef Stanislav Malyshev <smalyshev@gmail.com
>:

> Hi!
>
> > […]
> I
> > […]
>
> It's not forbidden now. But there's no reason to encourage it and
> develop a dedicated syntax for it. Current syntax works just fine. Yes,
> it produces a warning - and it should, since what is being done is what
> usually should not be done - ignoring exceptions.
>
> --
> Stas Malyshev
> smalyshev@gmail.com
