php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64910 Line number of $e = new Exception vs. line number of throw $e
Submitted: 2013-05-23 16:07 UTC Modified: 2013-05-23 20:34 UTC
Votes:10
Avg. Score:4.1 ± 1.3
Reproduced:10 of 10 (100.0%)
Same Version:6 (60.0%)
Same OS:7 (70.0%)
From: sebastian@php.net Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.5Git-2013-05-23 (Git) OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sebastian@php.net
New email:
PHP Version: OS:

 

 [2013-05-23 16:07 UTC] sebastian@php.net
Description:
------------
The error message that is created for an uncaught exception as well as the stacktrace of an exception list the number of the line on which the exception object was created. I would expect this to be number of the line on which the exception is raised using the throw statement.

Also note that the documentation on this is inconsistent: the Exception::getLine() method is documented with "Gets the line in which the exception occurred" whereas the Exception::$line attribute is documented with "The line where the exception was created".

Test script:
---------------
<?php
$e = new Exception;
throw $e;

Expected result:
----------------
Fatal error: Uncaught exception 'Exception' in /home/sb/test.php:3
Stack trace:
#0 {main}
  thrown in /home/sb/test.php on line 3

Actual result:
--------------
Fatal error: Uncaught exception 'Exception' in /home/sb/test.php:2
Stack trace:
#0 {main}
  thrown in /home/sb/test.php on line 2

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-23 17:20 UTC] derick@php.net
I'd agree with this. Seems like a fix could be to update the file and line properties of the exception in zend_throw_exception_internal/zend_throw_exception. Right now, it's set in the object init of the Exception class only.
 [2013-05-23 20:34 UTC] stas@php.net
Consider however this:

try {
	//stuff
} catch(Exception $e) {
	$logger->log("Oops, exception!");
	throw $e;
}

If we update file/line here, we lose original exception information and
file/line in the exception becomes useless. Right now, since 99.99% of
the code does "throw new", it is always useful. 

So how would you propose to solve this?
 [2013-05-23 21:33 UTC] theseer@php.net
Why would instantiating set a line/file info to begin with? I cannot come up with any usecase where I'd expect to get meaningful values from getLine() and getFile() merely upon instantiating an exception.

If neither would be set upon instantiating though, the "first" throw could simply check whether they are still NULL and if so, set them.

That way, there won't be any overriding and imho the expected behavior would be implemented?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 10:01:29 2024 UTC