Exceptions in PHP are always populated upon creation

Well, I've always wondered why the throw site wasn't in the stack
trace - this explains that.

This is inconsistent with at least JavaScript and C#, where the stack
trace is populated at the throw site. (Probably others?)

That doesn't really make any sense to me, for a number of reasons.

First, who cares where the Exception was constructed? That has no
relevance, and it's often a factory method or in some cases a more
complex facility, such as the one in Doctrine DBAL that maps PDO error
info to an Exception.

You will miss the actual throw site - unless it happens to be the same
site as where the constructor was invoked, but there's no guarantee of

When exceptions get re-thrown, there is no trace of the actual site
from where the unhandled Exception was thrown - you're lugging around
a stack trace that doesn't actually have the current stack.

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. What's important then, is the throw statement, not the
object, since the site where the object happens to get created has no
bearing on the stack at the time when the exception gets thrown or

In other words, throw new Exception() only happens to work most of the
time because the throw and new statements happen to be issued at the
same site.

This is wonky.

Can we fix it?

On Thu, May 19, 2016 at 3:05 PM, Niklas Keller wrote:
Sebastian Bergmann <sebastian@php.net> schrieb am Do., 19. Mai 2016 14:12:
Am 19.05.2016 um 13:14 schrieb Rasmus Schultz:
Does this work as intended?
According to https://bugs.php.net/bug.php?id=64910 it does :-(

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

Resending to complete list:

Yes, this is intended. Exceptions in PHP are always populated upon creation.

Please note the already mentioned issues with rethrowing and also, that
some exceptions might not get thrown at all, e.g. because of the use of

Search Discussions

Discussion Posts


Follow ups

Related Discussions

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



site design / logo © 2022 Grokbase