Posts Tagged ‘silly languages’

line numbers in php exceptions

Monday, October 20th, 2008

Quick geeky public service announcement. PHP 5’s Exception objects have line number and file name fields which are automatically populated for you. Refer to this page for details.

Ok, cool, but the question is: do those fields correspond to the construction site or the throw site? Based on this little experiment:

try {
	$exception = new Exception('exception constructed on '.__LINE__);
	throw $exception;
}
catch (Exception $e) {
	echo $e->getMessage().', (internal line:'.$e->getLine().')';
}

… they appear to refer to the construction site. In this case, the line number in the message (which is produced by the __LINE__ magic constant at the point of construction) and the return value of getLine() (made however it is the exception facility determines the line number) is the same number.

At first blush, this behavior seems less useful to me than if line numbers and file names were attached to the throw site. That’s where the exception actually occurs. It may be constructed entirely elsewhere. Hmmm.

Anyway, I couldn’t find an answer out there right away, at least with the terms I used to ask The Great Oracle. So here ’tis.