php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51067 Exception contains context from point of instantiation, not throw
Submitted: 2010-02-17 09:31 UTC Modified: 2010-02-17 11:01 UTC
From: Mateusz Przybylski <m dot przybylski at bkfmyjn Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.12 OS: linux
Private report: No CVE-ID: None
 [2010-02-17 09:31 UTC] Mateusz Przybylski <m dot przybylski at bkfmyjn
Description:
------------
An exception contains context (backtrace etc.) from the point it was
created, not where it was thrown.

I realize one could argue for either way, but IMHO it is more useful
to have it report point where it was thrown from than the place of
instantination.

Point in case: a function that translates certain error codes
returned from external system into exceptions. It makes no sense to
indicate the function itself as source of exceptional condition. The
function could be used by several functions interacting with the
remote system for common error signaling.

Example use: (NOT reproduce code)

function queryRemoteSystem($qry) {
	sendQry($qry);
	$r = readResponse();
	if ($r === false)
		throw remoteSystemException(); /* dis a function
call, yo */
}

function remoteSystemException() {
	switch (remoteErrorCode()) {
	case FOO:
		return new FooException();
	case BAR:
		return new BarException();
	default:
		return new GeneralRemoteException(); }
}



Reproduce code:
---------------
function creates($msg) {
	return new Exception($msg);
}

function throws() {
	throw creates('foo bar');
}

try {
	throws(); }
catch (Exception $e) {
	$trace = $e->getTrace();
	$firstFrame = $trace[0];
	echo 'Exception from function: '.$firstFrame['function']; }

Expected result:
----------------
Exception from function: throws

Actual result:
--------------
Exception from function: creates

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-17 10:43 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2010-02-17 11:01 UTC] Mateusz Przybylski <m dot przybylski at bkfmyjn
> Please double-check the documentation available at

That's what I did first -_-

The documentation of class Exception states explicitly:

(Property) line  The line where the exception was thrown

Which is not the case, as per this bug report -- it holds the line
the instance was created in, not where it was thrown. Same goes for
the backtrace -- it originates from the point the instance of
exception was created, not from where it was thrown.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Aug 08 12:00:03 2025 UTC