Stephen Coakley:

> This is the behavior in both Java and C#. Python has a different
> approach. Python has two properties that contain previous exceptions:
> __context__, which holds implicitly chained exceptions, and __cause__,
> which holds explicitly chained exceptions. This system makes a ton of
> sense, and I think PHP exceptions should distinguish between the two as
> well.
> 
> (Relevant PEP: <https://www.python.org/dev/peps/pep-3134/>)

Also thank you for that reference. As far as I understand this differs
in two aspects:

1) __context__ is a single exception opposed to having a list of
suppressed exceptions. This probably needs some more discussion to
decide the approach. Although I would prefer having a list of suppressed
exceptions I could imaging that as well.

A list of suppressed exceptions would make things like this possible:

} catch (Exception $e1) {
    foreach ($resources as $resource) {
        try {
            $resource->close();
        } catch (Exception $e2) {
            $e1->addSuppressed($e2);
        }
    }
    throw $e1;
}

And just in case if in some distant future one might propose something
like Java's try-with-resource PHP can easily do so without any BC break.

2) Caught exceptions will be considered as context as well. I'd say PHP
should not adopt that behaviour. First I'd like to understand the
semantic of suppressed as "a not caught exception". In Pythons world
it's called context, and therefore acceptable. But more than that this
leads to things like this:

try:
    raise Exception("exception 1")
except Exception as e:
    raise Exception("exception 2") from e

In this case exception 2 has exception 1 both as context and cause. I
think we don't need that information as we explicitly know by catching
the exception already the context.

Markus Malkusch

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to