Re: [RFC] Optional Catch Block Body
| From: | Nick | Date: | Thu, 31 Jul 2025 05:43:34 +0000 |
| Subject: | Re: [RFC] Optional Catch Block Body | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128321@lists.php.net to get a copy of this message | ||
> On 31. Jul 2025, at 11:53, Mihail Liahimov <91liahim@gmail.com> wrote:
>
> Introduction
>
> Currently, PHP requires a block body for catch clauses even when the caught exception is not
> used. This results in unnecessary boilerplate code. This RFC proposes allowing catch clauses without
> a body when the exception variable is omitted.
>
> Proposal
>
> Allow the following syntax where the curly braces can be omitted when no exception variable is
> specified and no handling is needed:
>
> try {
> // code that may throw
> } catch (SomeError);
>
> This would be equivalent to:
>
> try {
> // code that may throw
> } catch (SomeError) {}
>
> Motivation
>
> Reduced Boilerplate: Eliminates unnecessary empty blocks when exceptions only need to be caught
> and ignored.
> Improved Readability: Makes the code more concise and focuses on the important parts.
>
> Backward Incompatible Changes
>
> None. This is purely an additive change to the syntax.
>
Hey Mihail,
Why would the
catch (SomeError) still be required if it isn’t used?
Cheers,
Nick