Exception not being caught

Exception not being caught

am 15.07.2009 18:02:02 von Weston C

So, I've got a little piece of code designed to play with catching the
exception that's thrown when an object doesn't have a __toString
method.


class A { }

$a = new A(); // Ayn would be proud, right?

try {
echo "a is ",$a,"\n";
} catch(Exception $e) {
echo "\nException Caught: ";
echo $e, $n;
}

?>

This does not run as expected. I'd think that when the implicit string
conversion in the try block hits, the exception would be thrown,
caught by the catch block, and relayed.

Instead you don't ever see the words "exception caught" and you get
"Catchable fatal error: Object of class A could not be converted to
string."

If it's catchable, why isn't it caught in my example?

Thanks,

Weston

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Exception not being caught

am 16.07.2009 13:42:18 von David Otton

2009/7/15 Weston C :

> >
> class A { }
>
> $a =3D new A(); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // Ay=
n would be proud, right?
>
> try {
> =A0 =A0echo "a is ",$a,"\n";
> } catch(Exception $e) {
> =A0 =A0echo "\nException Caught: ";
> =A0 =A0echo $e, $n;
> }
>
> ?>
>
> This does not run as expected. I'd think that when the implicit string
> conversion in the try block hits, the exception would be thrown,
> caught by the catch block, and relayed.
>
> Instead you don't ever see the words "exception caught" and you get
> "Catchable fatal error: Object of class A could not be converted to
> string."
>
> If it's catchable, why isn't it caught in my example?

It's not an exception, it's a "fatal error". Fatal errors are caught
by error handling functions, not by catch blocks.

Consequence of having (at least) two separate error handling
mechanisms in the same language.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Exception not being caught

am 16.07.2009 15:38:27 von Martin Scotta

--001636283db871a1d2046ed2c942
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

There are many examples of error_handlers at
http://ar.php.net/manual/es/function.set-error-handler.php

The handler only have to cast the error to exception.

YOu can write a simple handler or a fully featured one, but the essence is
the same...

function errorHandler(/*args*/)
{
$e = new Exception( $message, $code );
throw $e;
}

On Thu, Jul 16, 2009 at 8:42 AM, David Otton <
phpmail@jawbone.freeserve.co.uk> wrote:

> 2009/7/15 Weston C :
>
> > > >
> > class A { }
> >
> > $a = new A(); // Ayn would be proud, right?
> >
> > try {
> > echo "a is ",$a,"\n";
> > } catch(Exception $e) {
> > echo "\nException Caught: ";
> > echo $e, $n;
> > }
> >
> > ?>
> >
> > This does not run as expected. I'd think that when the implicit string
> > conversion in the try block hits, the exception would be thrown,
> > caught by the catch block, and relayed.
> >
> > Instead you don't ever see the words "exception caught" and you get
> > "Catchable fatal error: Object of class A could not be converted to
> > string."
> >
> > If it's catchable, why isn't it caught in my example?
>
> It's not an exception, it's a "fatal error". Fatal errors are caught
> by error handling functions, not by catch blocks.
>
> Consequence of having (at least) two separate error handling
> mechanisms in the same language.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Martin Scotta

--001636283db871a1d2046ed2c942--

Re: Exception not being caught

am 16.07.2009 16:20:45 von Weston C

On Thu, Jul 16, 2009 at 7:42 AM, David
Otton wrote:
>> If it's catchable, why isn't it caught in my example?
>
> It's not an exception, it's a "fatal error". Fatal errors are caught
> by error handling functions, not by catch blocks.
>
> Consequence of having (at least) two separate error handling
> mechanisms in the same language.

That's definitely confusing, but more confusing to me is labeling the
fatal error as "catchable", which definitely implies a try / catch
block should handle it.

Maybe they should call it a "handleable" fatal error :) ... implying
it can be managed with set_error_handler().

On Thu, Jul 16, 2009 at 9:38 AM, Martin Scotta wrote:
....
> http://ar.php.net/manual/es/function.set-error-handler.php
....
> You can write a simple handler or a fully featured one, but the essence is
> the same...
>
> function errorHandler(/*args*/)
> {
> $e = new Exception( $message, $code );
> throw $e;
> }

This looks like a great idea -- I was thinking I'd have to give up the
convenient flow control involved in exception handling, but this
should bring it back nicely.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Exception not being caught

am 17.07.2009 19:12:04 von Lupus Michaelis

Weston C a écrit :

> If it's catchable, why isn't it caught in my example?

This is well-documented here


--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

Seeking for a position

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php