FAQ
Does this work as intended?

function create_exception() {
     return new RuntimeException(); // line 2
}

try {
     throw create_exception(); // line 6
} catch (Exception $e) {
     var_dump($e); // => 2
}

I would have expected Exception::getLine() to return 6 in this case -
the line where the exception was thrown.

I know that I can dig in via e.g.
Exception::getStackTrace()[0]["line"] and pull the relevant
line-number myself, but I'm wondering why the line-number 2 is
relevant, important or interesting at all?

It's not uncommon to have e.g. static factory methods inside the
exception itself, which would cause the exception instance to report
the line-number of the new-statement inside the factory constructor,
which has no particular importance or relevance for any purpose, as
far as I can figure.

Apparently this has been reported as a bug before:

https://bugs.php.net/bug.php?id=53882

There was talk of a fix in the comments, but it looks like this issue
went stale and was closed without any further explanation?

The documentation is also a little ambiguous on this point:

http://php.net/manual/en/exception.getline.php

At the top of the page, it says "Gets the line in which the exception
occurred", which isn't correct. (The rest of the page correctly says
"the line number where the exception was created")

Would it adversely affect anything to fix this?

It's technically a BC break, I guess.

Perhaps adding a dedicated getLineThrown() method would be better with
regards to BC?

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 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