FAQ
True, but if you instead of throwing the same exception, threw a new one, you could capture both stacks. But you can never get the stack from the throw point of something created elsewhere.
Precisely.

I think there are good reasons why other languages collect the
stack-trace on throw.

Collecting the stack trace at construction would be correct if "throw
new" was literally one statement - if the only time you could throw
was at creation.

That's not the case. We have deferred throws and factory methods for
exceptions, and we have re-throws, so collecting the stack-trace at
construction time doesn't work.

Of course, fixing this would require a BC break, and nobody wants to
even entertain the thought of those :-(
On Thu, May 19, 2016 at 10:30 PM, Ryan Pallas wrote:
On Thu, May 19, 2016 at 1:47 PM, Rowan Collins wrote:
On 19/05/2016 19:35, Rasmus Schultz wrote:

Technically, every throw is a new exception "flow" - even if you're >
recycling the Exception instance, it's the throw statement that >
starts the unique stack unwind from the throw site; it's where the >
action happens.

That's one interpretation, but it doesn't really hold up in all cases.
Consider a catch statement that needs to filter more granularly than the
class name; since you already mentioned PDO, I'll make an example with that:

catch ( PDOException $e ) {
if ( substr($e->getCode(), 0, 2) === '08' ) {
$this->reconnect();
} else {
throw $e;
}
}

Of what value to a subsequent catch statement is the trace of that throw
statement? And why does that "start a new exception flow", but if PDO threw
different sub-classes, you could let one flow through unmodified by
tightening the catch statement?
True, but if you instead of throwing the same exception, threw a new one,
you could capture both stacks. But you can never get the stack from the
throw point of something created elsewhere.

catch ( PDOException $e ) {
if ( substr($e->getCode(), 0, 2) === '08' ) {
$this->reconnect();
} else {
throw new PDOException($e->getMessage(), $e->getCode(), $e);
}
}


Regards,
--
Rowan Collins
[IMSoP]



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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 8 of 18 | next ›
Discussion Overview
groupphp-internals @
categoriesphp
postedMay 19, '16 at 11:14a
activeMay 24, '16 at 6:53p
posts18
users9
websitephp.net

People

Translate

site design / logo © 2022 Grokbase