PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Confused about catching runtime errors
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Confused about catching runtime errors

Réponse
 
LinkBack Outils de la discussion
Vieux 26/03/2008, 16h04   #1
Daniel Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Confused about catching runtime errors

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 ;-)

Daniel Klein
  Réponse avec citation
Vieux 26/03/2008, 16h51   #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
Vieux 26/03/2008, 17h40   #3
Daniel Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Confused about catching runtime errors

On Wed, 26 Mar 2008 16:51:36 +0100, "Rik Wasmus"
<luiheidsgoeroe@hotmail.com> wrote:

>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.


Thanks Rik; interesting reading, that.

I will look into type hinting but for now it appears as that isset() and the
is_xxxx() collection of functions will handle much of this error checking,
albeit it will require a bit more code than exception handling :-(

Btw, did you mean to post a different link to 'type hinting'? Perhaps you
meant
http://www.php.net/manual/en/languag...ypehinting.php

Daniel Klein
  Réponse avec citation
Vieux 27/03/2008, 03h20   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Confused about catching runtime errors

On Wed, 26 Mar 2008 17:40:56 +0100, Daniel Klein
<danielk@featherbrain.net> wrote:

> On Wed, 26 Mar 2008 16:51:36 +0100, "Rik Wasmus"
> <luiheidsgoeroe@hotmail.com> wrote:
>
>> 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 thatis
>>> 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.

>
> Thanks Rik; interesting reading, that.
>
> I will look into type hinting but for now it appears as that isset() and
> the
> is_xxxx() collection of functions will handle much of this error
> checking,
> albeit it will require a bit more code than exception handling :-(


Yup, also valuable the check for interfaces, allthough the instanceof
operator is prefered nowadays.

> Btw, did you mean to post a different link to 'type hinting'? Perhaps you
> meant
> http://www.php.net/manual/en/languag...ypehinting.php


Indeed, there seems to be wrong with either my copying or pasting of links
in my browser lately. That's the URL I ment.
--
Rik Wasmus
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16244 seconds with 12 queries