Hello, Rob.

>  I was quite surprised at how *impossible* it is.

```c
static zend_object *zend_default_exception_new(zend_class_entry
*class_type) /* {{{ */
{
....
if (EG(current_execute_data)) {
     zend_fetch_debug_backtrace(
```

It's not that this is 100% impossible. PHP is a language with an execution
context, which means PHP can hypothetically change behavior depending on
the context. This means it's entirely possible to imagine code like:
```php
if (EG(without_trace)) {}
```

One could follow Python's approach and create the backtrace only when the
exception is thrown. But this isn't a particularly elegant solution.
Besides, in 90% of cases, an exception is created to be thrown.

Use a deferred Backtrace generation algorithm? Such an algorithm is almost
impossible, because the backtrace is built based on the execution context,
which will be changed once the function returns control.

There are only two realistic options:

   -

   Either don't use a backtrace at all, or use a minimal version (only FILE
   + LINE).
   -

   Generate the backtrace only when the programmer explicitly requests it,
   but then accept that some information will be missing.

For example:

   -

   raise $exception — does not generate a backtrace.
   -

   throw $exception — generates a backtrace, but only from the point where
   the exception is thrown.

A lightweight backtrace might be imperfect in accurately indicating the
call path if multiple functions are on the same line, but otherwise it
saves a lot of memory and CPU.

The raise/throw option might also make sense. But it has more nuances and
contradictions.

Reply via email to