Re: Exceptions and builtin functions in PHP5
| From: | Tumurbaatar S. | Date: | Fri, 09 Apr 2004 11:07:14 +0000 |
| Subject: | Re: Exceptions and builtin functions in PHP5 | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-9024@lists.php.net to get a copy of this message | ||
Hi,
I simply thought that in case of direct using of predefined Exception
class, its $file and $line properties would point to my error handler
function.
I.e. where Exception object was created. Also its constructor does not
take file/line as param-s and these properties are read-only, so I thought
that there's no way to directly modify them.
"Marcus Boerger" <helly@php.net> wrote in message
news:13225082703.20040409114359@marcus-boerger.de...
> Hello Tumurbaatar,
>
> iam curios, why would you want to change the exceptions parameters
> $file and $line? They are both available through the backtrace at
> the second element. Try the following:
>
> [...]
> catch (Exception $e)
> {
> $trace = $e->getTrace();
> var_dump($trace[1]);
> }
>
> That outputs:
>
> array(3) {
> ["file"]=>
> string(36) "/usr/src/php-cvs/tests/lang/038.phpt"
> ["line"]=>
> int(26)
> ["function"]=>
> string(10) "pg_connect"
> }
>
> regards
> marcus
>
> Friday, April 9, 2004, 7:42:32 AM, you wrote:
>
> >>
> >> Perhaps this could be done in userland by throwing an exception in a
> >> user error_handler... did not test it though...
> >>
> > It seems that works! At least for E_WARNING:
>
> > class MyException extends Exception
> > {
> > function __construct($errno, $errstr, $errfile, $errline)
> > {
> > parent::__construct($errstr, $errno);
> > $this->file = $errfile;
> > $this->line = $errline;
> > }
> > }
>
> > function Error2Exception($errno, $errstr, $errfile, $errline)
> > {
> > throw new MyException($errno, $errstr, $errfile, $errline);
> > }
>
> > $err_msg = 'no exception';
> > set_error_handler('Error2Exception');
> > try
> > {
> > $con = pg_connect('host=localhost dbname=test');
> > }
> > catch (Exception $e)
> > {
> > $err_msg = sprintf('%x - %s', $e->getCode(), $e->getMessage());
> > }
>
>
>
>
>
> --
> Best regards,
> Marcus mailto:helly@php.net