> -----Original Message-----
> From: Sebastian Bergmann [mailto:sebast...@php.net] 
> Sent: 23 May 2013 21:18
> To: internals@lists.php.net
> Subject: [PHP-DEV] Bug #64910: Line number of $e = new 
> Exception vs. line number of throw $e
> 
>  Hi!
> 
>  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.
> 
>  Derick agrees with me that this is a bug. We propose to 
> update the file  and line properties of the exception object in the
>  zend_throw_exception_internal() and zend_throw_exception()
functions.
> 
>  Would such a change be accepted? Does it require an RFC?
> 

I agree that it should have been where the throw occurs, to allow for
an ExceptionFactory type use.

There is a method of making it better, but it's bit (rather a lot)
gnarly.

function createException($errno, $message, Exception $exception =
null)
{
  // Please forgive us for what we are about to do.
  // Fix up exceptions so they appear closer to where they are thrown
  $f = Closure::bind(
      function() {
        array_shift($this->trace);       // createException_
        $t = array_shift($this->trace);   // createException
        $this->line = $t['line'];
        $this->file = $t['file'];
        return $this;
      }, $this->createException_($errno, $message, $exception),
Exception::class);
  return $f();
}

private function createException_($errno, $message, Exception
$exception = null)
{
  switch ($errno)  {
    case 2002:
      return new ConnectException($message, $errno, $exception);

    case 2013:  // Connection dropped.
    case 2006:  // Gone away
      return new ConnectionDroppedException($message, $errno,
$exception);

    /*
    ....
    */

    default:
      return new DatabaseException($message, $errno, $exception);
  }
}

So

throw $obj->createException(...);  reports a more useful/accurate
location.

Jared


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to