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!
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
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?????
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".
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.
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
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.