Yes, this is the case, the exception object will populate its internal members at construction time, and having been thrown makes no alterations to internal members. This is good b/c a trace at the time of construction will persist until/or when the developer wishes to handle it.

I personally don't need line/file as much as exception type, so this has little affect on me. But to appease those that rely on file/line, why not add to methods to the Zend_Exception class (which extends exception and it is expected that all core exception classes derive from.

class Zend_Exception extends Exception
{
    ...
    final function getTraceLine($traceDepth = 1) { ... }
    final function getTraceFile($traceDepth = 1) { ... }
}

This will allow developers to lines and files from the trace, not the constructor. Bam, everyone happy ;)

-ralph


Bill Karwin wrote:
Okay I just tried this:

<?php
function testException()
{
   require_once 'Zend.php';
   try {
       $x = Zend::exception('Zend_Exception', 'boo!');
       throw $x;
   } catch (Zend_Exception $e) {
       echo "{$e->getMessage()}\n";
       echo "file: {$e->getFile()}\n";
       echo "line: {$e->getLine()}\n";
       echo "trace: {$e->getTraceAsString()}\n";
   }
}

testException();
?>


The output is:

--------
boo!
file: C:\zf\library\Zend.php
line: 229
trace: #0 C:\zf\library\testex.php(6): Zend::exception('Zend_Exception', 'boo!')
#1 C:\zf\library\testex.php(16): testException()
#2 {main}
--------

You're right, it isn't the line where it's thrown. It reports the file and line where the exception class is instantiated, inside the Zend::exception() function. But the stack trace shows where the Zend::exception() function was called.

I changed the it to the more traditional:

       throw new Zend_Exception('boo!');

And the output changed to this:

--------
boo!
file: C:\zf\library\testex.php
line: 8
trace: #0 C:\zf\library\testex.php(15): testException()
#1 {main}
--------

Regarding John's suggestion: the Exception class has no methods setFile() or setLine().

Regards,
Bill Karwin


Darby Felton wrote:
Hi all,

Zend::exception() only returns an exception; it does not currently throw
an exception, by design, to avoid such obfuscation:

throw Zend::exception('SomeException', 'Some Message');

Best regards,
Darby

Bill Karwin wrote:
Nico Edtinger wrote:
With Zend::exception() we avoid loading all those exception classes
that are only needed if an error occurs, which should be an exception.
So instead of loading these files or at least stat()ing it with an
opcode cache we waste some lines in a file that's loaded anyway. That
has nothing to do with bugs in any class.
But it's a good point that this method causes stack traces to become
obfuscated.  It reports the __FILE__ and __LINE__ of the exception as
occurring in Zend::exception, because that's where the exception is
actually thrown.

Regards,
Bill Karwin







Reply via email to