Re: [RFC] [Discussion] PREG_THROW_ON_ERROR
| From: | Osama Aldemeery | Date: | Thu, 30 Jul 2026 18:45:14 +0000 |
| Subject: | Re: [RFC] [Discussion] PREG_THROW_ON_ERROR | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-132143@lists.php.net to get a copy of this message | ||
On Thu, Jul 30, 2026 at 3:22 AM B.J.Scharp <
php.list.internals@bitwise-operators.com> wrote:
>
> On 28-07-2026 2:56 AM, Osama Aldemeery wrote:
> > Hi all,
>
> > The exception name is settled from the pre-RFC as
> >
\PregException
> > (thanks Tim) and the flag as PREG_THROW_ON_ERROR.
>
> From the RFC:
> > The flag covers both classes of PCRE failure.
>
> have you considered throwing different Exception types for the two
> failure types? (e.g. PregExecutionException and PregCompilationException)
>
>
> You could have both be child classes of PregException, but in actual
> fact, the second should probably be a child of LogicException, while the
> former really is a RuntimeException...
>
> Regards,
> Bernard
Hi Bernard,
Sorry, my last message got misformatted, so I am sending it again.
Thanks again for bringing this up.
As Rowan pointed out, the throwables policy settles half of it: additional
exceptions must extend the extension's base or each other, not the SPL
classes.
So the LogicException / RuntimeException parents are out regardless.
That leaves one PregException versus two under it (like your suggested
PregCompilationException / PregExecutionException), which is a judgment
call.
My starting point for the whole flag is that it only changes how an error
is delivered and nothing more.
That is the code and the message are exactly what preg_last_error() and
preg_last_error_msg() already give you.
A single exception sits most naturally on top of that.
There's a concrete nudge the same way....A compile failure only carries the
generic PREG_INTERNAL_ERROR / "Internal error" through the error
functions.
The real detail ("Compilation failed: ... at offset N") is in a warning,
not preg_last_error_msg().
So a PregCompilationException would carry "Internal error" and
little
else until that detail is surfaced properly, which I've left as future
scope.
The upside of two types is letting people catch the two failure modes
separately.
I'm not sure how often that's needed in practice (given that
pre_last_error() already does not differentiate between them), but I
don't object if people feel
it's the better design.
For now I'd still favor the single PregException, with the door open.
Thanks,
Osama