Afficher un message
Vieux 26/03/2008, 17h51   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Confused about catching runtime errors

On Wed, 26 Mar 2008 16:04:59 +0100, Daniel Klein
<danielk@featherbrain.net> wrote:

> When I run the following code the following errors end up in my error log
> file:
>
> PHP Notice: Undefined variable: n in C:\home\php\sandbox\error.php on
> line
> 14
> PHP Fatal error:Call to a member function fun() on a non-object in
> C:\home\php\sandbox\error.php on line 14
>
>
> <?php
> class Numpty {
> public function fun() {
> return 'Have fun!';
> }
> }
>
> $n = new Numpty();
> echo $n->fun();
>
> unset($n);
>
> try {
> echo $n->fun(); //this is line 14
> }
> catch (Exception $e) {
> echo 'Gotcha!';
> }
> echo 'Done';
> ?>
>
>
> You will notice that I have a 'try/catch' block around the line that is
> causing the error. Why doesn't this code catch the error?
>
> What is the PHP way of handling this situation?
>
> I'm reading up now on custom error handlers but I decided to post this
> now
> in case that leads to another wgc ;-)


http://bugs.php.net/bug.php?id=40014
When it's not a recoverable error error it will not be catchable in any
way. Adjust your code to cope with this by checking more / perhaps using
type hinting as in http://bugs.php.net/bug.php?id=40014.
--
Rik Wasmus
  Réponse avec citation
 
Page generated in 0,05262 seconds with 9 queries