Please Help::: FATAL Error handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zoltarb
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2003 10:27 am

Please Help::: FATAL Error handling

Post by zoltarb »

Hi,
Im a PHP newbie. could anyone please help me out regarding error handling in PHP. I've searched so many sites that talk abt Error handling, but when it comes to handling fatal errors theres nothing and even if there is, it is not that descriptive.
Being a Java programmer, its really frustrating when it comes to handling errors in PHP. But Im not giving up :)

regards,
zoltarb
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Have you read :

Ref : XXVI. Error Handling and Logging Functions
http://uk2.php.net/errorfunc

Ref : set_error_handler
http://uk2.php.net/manual/en/function.s ... andler.php

Regards,
zoltarb
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2003 10:27 am

Fatal Error Handling

Post by zoltarb »

Yes, I've looked into all the possible examples i could find on the net for handling fatal errors. but most of them are handled by trigger_error() functions. Isnt there any other way to capture these fatal errors and show the user with some message.
like for eg:

<?php
$test = 0;
while( true ){
$test *= 1000;
}

?>
(Ofcourse i wouldnt write such a code in my script, this is just to create some fatal error)Now when i execute this script it ofcourse gives me a fatal error for execution timeout.
Is there some way to capture this error and show some user friendly message?????

Thanks in advance.

Regards,
zoltarb
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Re: Fatal Error Handling

Post by cactus »

zoltarb wrote:Is there some way to capture this error and show some user friendly message?????
I do believe this is what you need, the only way (I know of) you can output non-standard error messages is with the use of:
cactus wrote:Ref : set_error_handler
http://uk2.php.net/manual/en/function.s ... andler.php
For example, if you had the script (in manual page above) as an include/require on all your pages, it would handle your exceptions, if you have "display_errors" set to "On".

Or am I misunderstanding your issue ?

Regards,
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

I think he is looking for something along the lines of a Try|Except block. As far as I know, there really is no good way of doing this in PHP 4 that is built-in. All the ones people have used have been home grown. One of the features of PHP 5 is the addition of a Try|Catch block that will allow you to do error checking and error handling much more easily than in the past.
zoltarb
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2003 10:27 am

Fatal Error Handling

Post by zoltarb »

Something like TRY|Catch exception handling is what I'm looking for. but since PHP4 doesnt support it, is there a workaround for that. I have tried including that script for set_error_handler in all the pages...but doesnt seem to work for fatal errors :(

Please Help!!
Thanks in advance

regards,
zoltarb
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Several things:

First, Exception handling will be in PHP5. Until then, your pretty much sunk.

Next, Fatal errors are fatal. "These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted." See, here the problem. Someone sets a script execution time of 30 seconds. Your script runs for 30 seconds with that loop. After that, it dies. It CAN'T let you handle the error, because then it would give back user control, and you could extend the execution time of the script, which would mean their's no sense in having a max execution time. However, you can always increase this time in php.ini.

Next, PHP has good error handling already. The problem is your trying to deal with fatal errors. You compare it to Java. Well, in PHP, a fatal error is basically the same thing as a Java programming not compiling to byte code.

If a Java program can't compile down to byte code, it won't run. PHP is sort of the same way. PHP can't run the error handler because the error is fatal. Fatal errors should NOT exist in production code. How could you catch an error for a missing semi-colon? It's a syntax error, it's fatal. It was never compiled to op-code, their is no way it could run.

Becareful about comparing PHP to Java. It's like comparing a 18-wheeler to a Mercedes. They do things differently.
zoltarb
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2003 10:27 am

Post by zoltarb »

thanks jason, guess will wait for PHP5 :)
Post Reply