# [RFC] Exceptions in the engine

- Thread: https://externals.io/message/77791
- Original archive: https://news-web.php.net/php.internals/77791
- Started by Nikita Popov on 2014-10-06T21:53:40+00:00
- Messages: 75
- RFC: Exceptions in the engine (Implemented (PHP 7.0)) — https://externals.io/rfc/engine_exceptions_for_php7.md
- RFC: Exceptions in the engine (Declined) — https://externals.io/rfc/engine_exceptions.md
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/77791.md?page=2
- 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

---

## #77791 — Nikita Popov — 2014-10-06T21:53:40+00:00

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

Hi internals!

During the PHP 5.6 development cycle I have proposed an RFC [1] that
suggested the use of exceptions instead of fatal errors in the engine. At
the time the proposal was declined, because the change was judged too
intrusive for a minor version.

As such I'm re-proposing this RFC for inclusion in PHP 7:

    https://wiki.php.net/rfc/engine_exceptions_for_php7

The RFC text is essentially the same as previously, with the primary
difference being that parse errors are now converted to exceptions as well.
This was previously not possible due to limitations in the compiler design.

Thanks,
Nikita

  [1]: https://wiki.php.net/rfc/engine_exceptions

---

## #77792 — Lars Strojny — 2014-10-06T22:12:31+00:00

URL: https://externals.io/message/77791#77792 · In reply to: #77791

Hi Nikita,


[...]
> As such I'm re-proposing this RFC for inclusion in PHP 7:
> 
>    https://wiki.php.net/rfc/engine_exceptions_for_php7
> 
> The RFC text is essentially the same as previously, with the primary
> difference being that parse errors are now converted to exceptions as well.
> This was previously not possible due to limitations in the compiler design.

I very much like the idea of making fatal errors exceptions, so: thumbs up! Is there a way to introduce different exception classes for different errors. E.g.

 - UndefinedMethodException for stdClass::method()
 - NullPointerException for null::method()
 - UndefinedFunctionException for invalid_function()
 - etc.

They could all extend EngineException to have a common base class but I am sure we could find good abstractions for an exception inheritance tree.

What do you think?

cu,
Lars

---

## #77793 — Stas Malyshev — 2014-10-06T23:07:23+00:00

URL: https://externals.io/message/77791#77793 · In reply to: #77791

Hi!

> As such I'm re-proposing this RFC for inclusion in PHP 7:
> 
>     https://wiki.php.net/rfc/engine_exceptions_for_php7
> 
> The RFC text is essentially the same as previously, with the primary
> difference being that parse errors are now converted to exceptions as well.
> This was previously not possible due to limitations in the compiler design.

I like this. Fatal errors and warnings were for a long time in different
leagues, and the need to handle them better produced `E_RECOVERABLE_ERROR` 
which is a weird creature - like exception but not quite. I think PHP 7
is a good time to move to exceptions for real errors (as opposed to
informational messages like E_WARNING).

The only issue I think we need to discuss is catch(Exception $e). Now it
would catch much more than before, if we do no changes. Should we allow
it or should be have hierarchy that separates user exceptions and engine
exceptions, and have catch(Exception) catch only the former.

However, I see in the RFC it does not remove all the errors. I think we
need to strive for having all the non-core errors except for out of
memory and timeout be converted. Are there ones that are problematic to
convert or it is just the question of time/effort?

About the long error message - I usually wouldn't recommend that but
maybe here is a good place for an ini setting, saying if we really want
to display the stack trace for uncaught exceptions? In many production
systems, wasting time to collect and then print the backtrace may not be
needed, and one-point message is enough. Many tools already have such
option (xdebug included) so maybe if we move to exceptions core may have
something like that too?
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77794 — Dan Ackroyd — 2014-10-07T08:55:17+00:00

URL: https://externals.io/message/77791#77794 · In reply to: #77793

Stas wrote:

> The only issue I think we need to discuss is catch(Exception $e). Now it
> would catch much more than before, if we do no changes.

It's not clear why would that be an issue - can you specify what the
problem would be?

Also, if we changed `catch(Exception $e)` to not catch all exceptions,
than we would need to have another way of specifying that a catch
block should catch all exceptions. Which would involve either making
\Exception extend another even 'baser' Exception, or a hack to the
syntax e.g. something like:

catch($e) {
    // Catch without Exception type catches all exceptions
    // and confuses people.
}

cheers
Dan
Ack


> Hi!
>
> > […]
>
> I like this. Fatal errors and warnings were for a long time in different
> leagues, and the need to handle them better produced `E_RECOVERABLE_ERROR` 
> which is a weird creature - like exception but not quite. I think PHP 7
> is a good time to move to exceptions for real errors (as opposed to
> informational messages like E_WARNING).
>
> The only issue I think we need to discuss is catch(Exception $e). Now it
> would catch much more than before, if we do no changes. Should we allow
> it or should be have hierarchy that separates user exceptions and engine
> exceptions, and have catch(Exception) catch only the former.
>
> However, I see in the RFC it does not remove all the errors. I think we
> need to strive for having all the non-core errors except for out of
> memory and timeout be converted. Are there ones that are problematic to
> convert or it is just the question of time/effort?
>
> About the long error message - I usually wouldn't recommend that but
> maybe here is a good place for an ini setting, saying if we really want
> to display the stack trace for uncaught exceptions? In many production
> systems, wasting time to collect and then print the backtrace may not be
> needed, and one-point message is enough. Many tools already have such
> option (xdebug included) so maybe if we move to exceptions core may have
> something like that too?
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/

---

## #77795 — Ferenc Kovacs — 2014-10-07T09:32:05+00:00

URL: https://externals.io/message/77791#77795 · In reply to: #77794

> Stas wrote:
>
> > […]
>
> It's not clear why would that be an issue - can you specify what the
> problem would be?
>
> Also, if we changed `catch(Exception $e)` to not catch all exceptions,
> than we would need to have another way of specifying that a catch
> block should catch all exceptions. Which would involve either making
> \Exception extend another even 'baser' Exception, or a hack to the
> syntax e.g. something like:
>
> catch($e) {
>     // Catch without Exception type catches all exceptions
>     // and confuses people.
> }
>


we discussed this before on the mailing list, the problem would be that
currently the fatal errors will stop the execution and even recoverable
fatals for most people (when they don't have a global error handler defined
to handle it) and theoretically it is possible that some code exists out
there with Pokemon Exception Handling (
http://c2.com/cgi/wiki?PokemonExceptionHandling) where the code would
continue the execution and probably does some logically faulty operation
which previously was only prevented by the fatal error stopping the code.
this indeed assumes that the user already follow bad coding practices(Catch
them all) and code with (potential) fatal errors so there were a
disagreement if we could afford this BC or not.
Personally I think that it would be fine for a major version.

>
>
-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #77796 — Christian Stoller — 2014-10-07T09:51:39+00:00

URL: https://externals.io/message/77791#77796 · In reply to: #77794

From: Dan Ackroyd [mailto:danack@basereality.com] ,Sent: Tuesday, October 07, 2014 10:55 AM
> 
> Stas wrote:
>
> > […]
>
> It's not clear why would that be an issue - can you specify what the
> problem would be?
>
> Also, if we changed `catch(Exception $e)` to not catch all exceptions,
> than we would need to have another way of specifying that a catch
> block should catch all exceptions. Which would involve either making
> \Exception extend another even 'baser' Exception, or a hack to the
> syntax e.g. something like:
> 
> catch($e) {
>     // Catch without Exception type catches all exceptions
>     // and confuses people.
> }
>
> cheers
> Dan
> Ack

We could make EngineException to be parent of the base Exception class.
And, if it is technically possible, it must be forbidden to extend
EngineException directly in userland.

This is just an idea. It would be a little bit strange if Exception is
not the base exception class ;) but it makes sense, because EngineException
is an exception of all other exceptions ^^

Christian

---

## #77797 — Dmitry Stogov — 2014-10-07T11:25:32+00:00

URL: https://externals.io/message/77791#77797 · In reply to: #77791

Hi Nikita,

I very like the idea.
Actually, it's a long time desired php feature that many big php users miss.
They don't like to show 500 response in any case.

Also +1 for removing ability of silent `E_RECOVERABLE_ERROR` bypass :)

I think that few things may be "improved"

1) I'm not sure if "cacth(Exception $e)" should catch engine and parser
exceptions. May be it's better to introduce interface or common parent
class "Catchable" and then make Exception, EngineException and
ParseException implement it. In case user would like to catch all the error
at once they will able to use "catch(Catchable $e)", but cacth(Exception
$e) would be 100% compatible.

2) I also think it's not a big problem to keep compatible error messages
for uncaught EngineException and ParseException. I'm not sure if that
"Uncaught EngineException:" really need.

Thanks. Dmitry.






> Hi internals!
>
> During the PHP 5.6 development cycle I have proposed an RFC [1] that
> suggested the use of exceptions instead of fatal errors in the engine. At
> the time the proposal was declined, because the change was judged too
> intrusive for a minor version.
>
> As such I'm re-proposing this RFC for inclusion in PHP 7:
>
>     https://wiki.php.net/rfc/engine_exceptions_for_php7
>
> The RFC text is essentially the same as previously, with the primary
> difference being that parse errors are now converted to exceptions as well.
> This was previously not possible due to limitations in the compiler design.
>
> Thanks,
> Nikita
>
>   [1]: https://wiki.php.net/rfc/engine_exceptions

---

## #77798 — Ferenc Kovacs — 2014-10-07T12:15:55+00:00

URL: https://externals.io/message/77791#77798 · In reply to: #77796

On Tue, Oct 7, 2014 at 11:51 AM, Christian Stoller <stoller@leonex.de>
wrote:

> From: Dan Ackroyd [mailto:danack@basereality.com] ,Sent: Tuesday, October
> 07, 2014 10:55 AM
> > […]
>
> We could make EngineException to be parent of the base Exception class.
> And, if it is technically possible, it must be forbidden to extend
> EngineException directly in userland.
>
> This is just an idea. It would be a little bit strange if Exception is
> not the base exception class ;) but it makes sense, because EngineException
> is an exception of all other exceptions ^^
>
> Christian
>

yes, this was also suggested before, but that will be also a BC break for
those people already using the name of the new parent class (
https://github.com/search?l=php&q=EngineException&type=Code&utf8=%E2%9C%93
for example).
which can be still an ok decision, I'm just stating/repeating the pro/cons
as this was all discussed before.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #77799 — Ralf Lang — 2014-10-07T13:43:15+00:00

URL: https://externals.io/message/77791#77799 · In reply to: #77791

> Hi internals!
> 
> During the PHP 5.6 development cycle I have proposed an RFC [1] that
> suggested the use of exceptions instead of fatal errors in the engine. At
> the time the proposal was declined, because the change was judged too
> intrusive for a minor version.
> 
> As such I'm re-proposing this RFC for inclusion in PHP 7:
> 
>     https://wiki.php.net/rfc/engine_exceptions_for_php7

++

I very much like the change and have proposed something similar before.
A major version change is the best time to do it and it will be long
after php7 to see another one.

This would enable us to cleanup much weird userland code.

Just let's make sure to get it right this time.


-- 
Ralf Lang
Linux Consultant / Developer
Tel.: +49-170-6381563
Mail: lang@b1-systems.de
B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537

---

## #77800 — Rowan Collins — 2014-10-07T13:58:12+00:00

URL: https://externals.io/message/77791#77800 · In reply to: #77792

Lars Strojny wrote (on 06/10/2014):
> Hi Nikita,
>
> 
> [...]
> > […]
> I very much like the idea of making fatal errors exceptions, so: thumbs up! Is there a way to introduce different exception classes for different errors. E.g.
>
>   - UndefinedMethodException for stdClass::method()
>   - NullPointerException for null::method()
>   - UndefinedFunctionException for invalid_function()
>   - etc.
>
> They could all extend EngineException to have a common base class but I am sure we could find good abstractions for an exception inheritance tree.
>
> What do you think?

It was suggested in the previous discussion that this could be added 
later, but IMHO, it's a must. Part of the power of exceptions is that 
you can *selectively* catch them *when you know you can deal with the 
result*.

Emitting a single EngineException will just encourage a kind of "pokemon 
exception handling", because the only way to use it is in a generic 
"something went wrong" handler. The semantics of that handler might be a 
bit tidier than one for E_RECOVERABLE_ERROR, but there's not a lot of 
extra power available.

As an example where a specific exception would be useful, but a generic 
one would not, consider a fluent interface which sometimes bugs out and 
returns NULL. If wrapped in a NullPointerException these expected error 
conditions could be caught at runtime, while any other errors would 
continue to have their normal behaviour (such as triggering a 
higher-scoped catch-all/default exception handler).

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #77813 — Markus Fischer — 2014-10-07T17:40:52+00:00

URL: https://externals.io/message/77791#77813 · In reply to: #77798

> yes, this was also suggested before, but that will be also a BC break for
> those people already using the name of the new parent class (
> https://github.com/search?l=php&q=EngineException&type=Code&utf8=%E2%9C%93
> for example).
> which can be still an ok decision, I'm just stating/repeating the pro/cons
> as this was all discussed before.

It should be considered that most of the results of this links are using
namespaces, the minority doesn't. And those ask for trouble anyway,
IMHO, if they don't use any kind of namespacing (be it old PEAR
conventions or using real namespaces).

- Markus

---

## #77814 — Levi Morrison — 2014-10-07T17:52:35+00:00

URL: https://externals.io/message/77791#77814 · In reply to: #77800

> Lars Strojny wrote (on 06/10/2014):
>
> > […]
>
>
> It was suggested in the previous discussion that this could be added later,
> but IMHO, it's a must. Part of the power of exceptions is that you can
> *selectively* catch them *when you know you can deal with the result*.
>
> Emitting a single EngineException will just encourage a kind of "pokemon
> exception handling", because the only way to use it is in a generic
> "something went wrong" handler. The semantics of that handler might be a bit
> tidier than one for E_RECOVERABLE_ERROR, but there's not a lot of extra
> power available.
>
> As an example where a specific exception would be useful, but a generic one
> would not, consider a fluent interface which sometimes bugs out and returns
> NULL. If wrapped in a NullPointerException these expected error conditions
> could be caught at runtime, while any other errors would continue to have
> their normal behaviour (such as triggering a higher-scoped catch-all/default
> exception handler).

I would contend that catching a NullPointerException is not any more
recoverable than most other EngineExceptions. I would also argue that
you should never, in any language, catch a NPE. The only time you
should catch an NPE is if you are catching all exceptions, such as in
a FastCGI daemon that will cleanly shutdown afterwards.

---

## #77815 — Stas Malyshev — 2014-10-07T17:55:07+00:00

URL: https://externals.io/message/77791#77815 · In reply to: #77798

Hi!

> yes, this was also suggested before, but that will be also a BC break
> for those people already using the name of the new parent class
> (https://github.com/search?l=php&q=EngineException&type=Code&utf8=%E2%9C%93
> for example).

This btw looks like a bigger issue (though many of them are namespaced).
Maybe we shoud do \PHP\EngineException?

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77816 — Stas Malyshev — 2014-10-07T18:00:21+00:00

URL: https://externals.io/message/77791#77816 · In reply to: #77797

Hi!

> 1) I'm not sure if "cacth(Exception $e)" should catch engine and parser
> exceptions. May be it's better to introduce interface or common parent
> class "Catchable" and then make Exception, EngineException and
> ParseException implement it. In case user would like to catch all the error
> at once they will able to use "catch(Catchable $e)", but cacth(Exception
> $e) would be 100% compatible.

We had some requests for 'Throwable' class/interface for some time, e.g.:
https://bugs.php.net/bug.php?id=48599

Maybe we could borrow something from Java here
(http://docs.oracle.com/javase/6/docs/api/java/lang/Throwable.html) -
not sure if it's the best way but it's an option.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77819 — Rowan Collins — 2014-10-07T19:46:29+00:00

URL: https://externals.io/message/77791#77819 · In reply to: #77814

>On Tue, Oct 7, 2014 at 7:58 AM, Rowan Collins <rowan.collins@gmail.com>
>wrote:
> > […]
>generic one
> > […]
>returns
> > […]
>conditions
> > […]
>have
> > […]
>catch-all/default
> > […]
>
>I would contend that catching a NullPointerException is not any more
>recoverable than most other EngineExceptions. 

In the scenario I just listed, the catch is effectively emulating an isset()/is_object() check before each chained operation. Seems perfectly reasonable to me. (If I'm not much mistaken, CoffeeScript has an operator for this precise case.)

> I would also argue that
>you should never, in any language, catch a NPE. The only time you
>should catch an NPE is if you are catching all exceptions, such as in
>a FastCGI daemon that will cleanly shutdown afterwards.

If you're not allowed to catch it, why make it an exception? Benefits such as unwinding finally blocks and destructors, and collecting backtraces, are secondary effects, not really fundamental to exceptions.

Perhaps "NullPointerException" is not a good name, as it has rather different implications than in other languages. In PHP, using the -> operator on a non-object isn't much different from calling a function with incorrect types. It shouldn't happen if your code is properly unit-tested, but it doesn't necessarily mean a catastrophic failure has occurred.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #77820 — Levi Morrison — 2014-10-07T20:07:08+00:00

URL: https://externals.io/message/77791#77820 · In reply to: #77819

> > […]
>
> If you're not allowed to catch it, why make it an exception? Benefits such as unwinding finally blocks and destructors, and collecting backtraces, are secondary effects, not really fundamental to exceptions.

This is not a new idea; a very quick search turned up the CERT Oracle
Coding Standard for Java:

"Programs must not catch java.lang.NullPointerException. A
NullPointerException exception thrown at runtime indicates the
existence of an underlying null pointer dereference that must be fixed
in the application code[...] Handling the underlying null pointer
dereference by catching the NullPointerException rather than fixing
the underlying problem is inappropriate for several reasons."

I'll let you read the standard for the justification.

The point is that a few applications (and I mean a *very* few) need to
be able to handle these kinds of failures; for example a web server
shouldn't crash because the application throws a NullPointerException;
rather the worker dies and the client will be sent a 500 error and the
web server continues to run normally.

---

## #77821 — Rowan Collins — 2014-10-07T23:16:19+00:00

URL: https://externals.io/message/77791#77821 · In reply to: #77820

> > […]
> This is not a new idea; a very quick search turned up the CERT Oracle
> Coding Standard for Java:
>
> "Programs must not catch java.lang.NullPointerException. A
> NullPointerException exception thrown at runtime indicates the
> existence of an underlying null pointer dereference that must be fixed
> in the application code[...] Handling the underlying null pointer
> dereference by catching the NullPointerException rather than fixing
> the underlying problem is inappropriate for several reasons."
>
> I'll let you read the standard for the justification.

Assuming you mean this (you forgot the link, so I searched the quote): 
https://www.securecoding.cert.org/confluence/display/java/ERR08-J.+Do+not+catch+NullPointerException+or+any+of+its+ancestors

They give 3 reasons:

1) Performance overhead.
I have no idea whether this is true for PHP's implementation of 
Exceptions, or for the scenario which I came up with earlier.

2) Inability to know which part of the try{} block initiated the error.
This is a fair point, although why it is more true of these exceptions 
than any other, I'm not sure.

3) "programs rarely remain in an expected and usable state after a 
|NullPointerException| has been thrown"
As mentioned earlier, PHP is not Java, and the closest to a "Null 
Pointer" is actually "variable which you thought was going to be an 
object but turned out to be a scalar or array". The situations that lead 
to the exception are vastly different, so the chances of recoverability 
aren't really generalisable from one to the other.

To clarify, this is the kind of code I was thinking of:

$result = DataSource::newInstance()
    ->select('foo')
    ->from('bar')
    ->with('baz') // WHAT IF THIS RETURNS FALSE?
    ->using('id')
    ->where('foo_name', $name)
    ->and('active', true)
    ->fetchArray();

There are two ways I know of for dealing with an error midway through a 
chain like that:
1) Return a "Null Object", or an object which has been put into an error 
state, so that all following calls are executed, but do nothing.
2) Throw an exception, so that the chain finishes early, and the calling 
code knows an error has occurred.

If the library author has done neither of these, you will be faced with 
a "call to a member-function on a non-object" error. Your only recourse 
if you cannot / are unwilling to fix the library is to remove the 
elegance of the fluent interface and check an intermediate variable with 
 `is_object()` or instanceOf on every line.

If that was an exception, then you, as the library consumer, could catch 
it, just like you would catch a custom exception thrown by a more 
conscientious library author.

> The point is that a few applications (and I mean a*very*  few) need to
> be able to handle these kinds of failures; for example a web server
> shouldn't crash because the application throws a NullPointerException;
> rather the worker dies and the client will be sent a 500 error and the
> web server continues to run normally.

Just to clarify, are you broadly in favour of Engine Exceptions, or 
against the whole idea? Because this sounds like a call for something 
completely separated from the userland notion of "Exception", and indeed 
something that would rarely even be written in PHP rather than C in an 
extension.

Regards,

-- 
Rowan Collins
[IMSoP]

---

## #77846 — Nikita Popov — 2014-10-10T11:46:47+00:00

URL: https://externals.io/message/77791#77846 · In reply to: #77793

On Tue, Oct 7, 2014 at 1:07 AM, Stas Malyshev <smalyshev@sugarcrm.com>
wrote:

> Hi!
>
> > […]
> well.
> > […]
> design.
>
> I like this. Fatal errors and warnings were for a long time in different
> leagues, and the need to handle them better produced `E_RECOVERABLE_ERROR` 
> which is a weird creature - like exception but not quite. I think PHP 7
> is a good time to move to exceptions for real errors (as opposed to
> informational messages like E_WARNING).
>
> The only issue I think we need to discuss is catch(Exception $e). Now it
> would catch much more than before, if we do no changes. Should we allow
> it or should be have hierarchy that separates user exceptions and engine
> exceptions, and have catch(Exception) catch only the former.
>

There is precedent in other languages for having exceptions that are not
usually caught. Java has a Throwable interface, which is implemented by
Exceptions and Errors, where the latter are usually not supposed to be
caught. Python has a BaseException, which is extended by Exception and
SystemExit, GeneratorExit and KeyboardInterrupt, where the latter three are
not usually supposed to be caught.

I think that the decision whether or not we wish to introduce something
like this mainly depends on how we intend these EngineExceptions to be
used. If they are only supposed to be caught by top-level code to enable
graceful error handling (in particular for long running code) then it
probably makes sense to keep them outside the main exception hierarchy. If
on the other hand it's considered okay to catch them during normal program
execution, they should be normal exceptions.


> However, I see in the RFC it does not remove all the errors. I think we
> need to strive for having all the non-core errors except for out of
> memory and timeout be converted. Are there ones that are problematic to
> convert or it is just the question of time/effort?
>

There are a number of E_ERRORs that we cannot feasibly convert to
exceptions. I've analyzed this for the previous discussion and likely the
numbers are still about right: http://markmail.org/message/gfdczr6zpgzynid4
So a conservative estimate is that we can turn 85% of the `E_ERROR` 
occurrences into exceptions. (This doesn't tell us anything about the
percentage of actually thrown errors though.)

And yes, it's also a question of time/effort. The hard part usually isn't
converting to exceptions, but updating all the tests that fail because of
it. I'd like to have good exception coverage by the time PHP 7 gets
released, but would prefer to do the porting incrementally (these huge
patches get stale very fast).

Nikita

---

## #77850 — Patrick Schaaf — 2014-10-10T12:31:09+00:00

URL: https://externals.io/message/77791#77850 · In reply to: #77846

Looking at the list of fatal errors Nikita classified as not suitable for
converting to exceptions, I'd like to take exception : ) with two of them:

I would love to be able to catch, at toplevel. with an error handler or
otherwise, both the memory limits exceeded and time limit exceeded cases.

These two safeguards, by themselves, are not instabilities, right? The
limits could be set a bit higher and everything would continue normally,
right?

What I would like to see, is that I can configure an "extra allowance" for
both of these limits, in php.ini - if that extra allowance is configured,
and the normal limit is reached, generate a recoverable / catchable error
and/or exception, and extend the respective limit (once) by the extra
allowance.

So, in practise, I would allow 2 more seconds, and 10 MB more memory, for
use in a global handler that gives nicer error message, backtrace,
non-white-http-500-response, or similar reaction.

best regards
  Patrick

---

## #77851 — Dan Ackroyd — 2014-10-10T12:52:10+00:00

URL: https://externals.io/message/77791#77851 · In reply to: #77850

Patrick wrote:
> I would love to be able to catch, at toplevel. with an error handler or
> otherwise, both the memory limits exceeded and time limit exceeded cases.

+1, as the kids today say.

I don't think that it should require any new code - we already have
the ability to extend the memory limit dynamically:

ini_set('memory_limit', (ini_get('memory_limit')+1).'M');

And similarly for `set_time_limit()`.

Having the ability to catch these types of errors and do whatever is
required to exit the program cleanly, is far superior to having a
fatal error.

cheers
Dan

---

## #77858 — Andrea Faulds — 2014-10-10T20:43:12+00:00

URL: https://externals.io/message/77791#77858 · In reply to: #77850

> I would love to be able to catch, at toplevel. with an error handler or
> otherwise, both the memory limits exceeded and time limit exceeded cases.
> 
> These two safeguards, by themselves, are not instabilities, right? The
> limits could be set a bit higher and everything would continue normally,
> right?

Both are problematic.

There’s no *technical* reason to prevent catching the exceeding of the time limit… but that means you can circumvent the time limit. That’s bad. Perhaps you could catch it and have a short window to output an error and die, though?

Memory limit catching *would* cause instability, though. That’s an error thrown during memory allocation. Things will break horribly if it just returns `NULL` and carries on, because let’s be realistic here, an awful lot of code doesn’t check for `NULL` return values. It’ll lead to a segfault. PHP must bail out immediately when this happens to prevent segfaulting.
--
Andrea Faulds
http://ajf.me/

---

## #82648 — Nikita Popov — 2015-02-13T23:25:12+00:00

URL: https://externals.io/message/77791#82648 · In reply to: #77791

> Hi internals!
>
> During the PHP 5.6 development cycle I have proposed an RFC [1] that
> suggested the use of exceptions instead of fatal errors in the engine. At
> the time the proposal was declined, because the change was judged too
> intrusive for a minor version.
>
> As such I'm re-proposing this RFC for inclusion in PHP 7:
>
>     https://wiki.php.net/rfc/engine_exceptions_for_php7
>
> The RFC text is essentially the same as previously, with the primary
> difference being that parse errors are now converted to exceptions as well.
> This was previously not possible due to limitations in the compiler design.
>
> Thanks,
> Nikita
>
>   [1]: https://wiki.php.net/rfc/engine_exceptions
>

Feature freeze is not so far away now, so I'd like to bring this RFC up
again and proceed to voting shortly. There are two primary open questions:

 * Subclassing: Should there be more specific subclasses of EngineException
for particular errors?
 * Superclassing: Should EngineException inherit from Exception (and as
such be subject to catch(Exception)) or should we introduce some kind of
special super-class that is not caught by default (like
Catchable/Throwable)?

I don't think we can implement a high-quality subclassing scheme in a
timeframe for PHP 7, as such I would suggest to postpone this (if we
actually want to do it) to a later point in time. We can introduce
subclasses without BC issues in a minor version.

The question of whether EngineException should inherit from Exception is
something we do have to consider now. Personally I prefer not introducing
any special exception types that aren't caught by default. I think that
would only make sense for errors that could occur literally everywhere
(like memory limit or timeout), but these are not handled by this RFC for
technical reasons. If someone has a strong opinion on this, I might make it
a voting option.

Commentary on these, and also any other relevant points is very welcome!

Thanks,
Nikita

PS: The patch attached to the RFC is very outdated. I plan to only update
it to current master once the RFC passes (if it does), as I already had to
practically rewrite it a few times.

---

## #82651 — Yasuo Ohgaki — 2015-02-14T00:09:55+00:00

URL: https://externals.io/message/77791#82651 · In reply to: #77791

Hi Nikita,



> As such I'm re-proposing this RFC for inclusion in PHP 7:
>
>     https://wiki.php.net/rfc/engine_exceptions_for_php7
>
> The RFC text is essentially the same as previously, with the primary
> difference being that parse errors are now converted to exceptions as well.
> This was previously not possible due to limitations in the compiler design.
>

Are E_WARNING, etc future scope?
I briefly looked the patch. It seems it covers only engine errors.
I suppose it's the scope of this RFC, though.
If API is made adoptable to modules for E_WARNING, etc, it would be
great.

Anyway, +1

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #82652 — Nikita Popov — 2015-02-14T00:15:13+00:00

URL: https://externals.io/message/77791#82652 · In reply to: #82651

> Hi Nikita,
>
> 
>
> > […]
>
> Are E_WARNING, etc future scope?
> I briefly looked the patch. It seems it covers only engine errors.
> I suppose it's the scope of this RFC, though.
> If API is made adoptable to modules for E_WARNING, etc, it would be
> great.
>

This RFC is strictly about fatal and recoverable fatal errors. Changing any
other error types to exceptions would be a significant
backwards-compatibility break.

Nikita


> Anyway, +1
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #82653 — Benoit Schildknecht — 2015-02-14T00:32:01+00:00

URL: https://externals.io/message/77791#82653 · In reply to: #82648

Hi,

Le Sat, 14 Feb 2015 00:25:12 +0100, Nikita Popov <nikita.ppv@gmail.com> a  
écrit:

> The question of whether EngineException should inherit from Exception is
> something we do have to consider now. Personally I prefer not introducing
> any special exception types that aren't caught by default. I think that
> would only make sense for errors that could occur literally everywhere
> (like memory limit or timeout), but these are not handled by this RFC for
> technical reasons. If someone has a strong opinion on this, I might make  
> it
> a voting option.
>
> Commentary on these, and also any other relevant points is very welcome!
>
> Thanks,
> Nikita
>

I don't think EngineException should inherit from Exception, it would be  
very dangerous. I'm sure a lot of exceptions are handled like this by a  
lot of devs, without creating custom exceptions :

try {

} catch ( Exception $e ) {

}

Making EngineException inherit from Exception means that there is a chance  
it will change the behavior of this kind of code, maybe breaking some  
crucial datas in database or such.

Instead, I think Exception should inherit from EngineException. Or  
anything else.

Regards,
Benoit.

---

Next page: https://externals.io/message/77791.md?page=2
