[PHP]Need help with error handling (Specifically Try / Catch statements).

Experienced Elementalist
Joined
Jul 4, 2004
Messages
211
Reaction score
0
I am trying to write a try / catch statement to catch any error (Exception $e) and then detect if debug is on, or if the user is admin and can view errors, if they arent it just says something was wrong and documents the error and notifies me.

However, the try / catch will not catch any errors. Such as:
PHP:
<?php
try 
{
    echo "what"
} catch (Exception $e)
{
    if (user is admin || debug is on)
    {
        print the error and maybe some debug info.
    } else
    {
        echo "Print some basic *There was a problem* error message, and that the Admin has been notified";
    }
}
?>
As you can see, I am intentionally trying to break it by leaving the ; off of the echo statement, however, it does not catch this error, rather display the error message and move on.

I am told I do not need to throw an exception for try / catch to work, and that it should catch stuff like this, can anyone confirm this and maybe say what I am doing wrong?

Thanks.

PS: Should be noted I am using PHP5.2.3.
 
If memory serves, you can do it like this but only by overwriting PHP's error handling class yourself. See also set_error_handler().

On default, PHP will handle the error itself and you must specifically throw an exception. This is also the desired behaviour since otherwise you would get runtime errors when thrown exceptions aren't cought.
 
If memory serves, you can do it like this but only by overwriting PHP's error handling class yourself. See also set_error_handler().

On default, PHP will handle the error itself and you must specifically throw an exception. This is also the desired behaviour since otherwise you would get runtime errors when thrown exceptions aren't cought.

Thanks for the reply :)

Would I still need to use a try / catch? or when an error occurred would it pass it into my error handler and handle it that way? (Looking at the documentation on that function you linked, thats what im getting.).

Anyway, going to start writing one now.


edit: Ok, got it working :) Thanks.
 
Last edited:
I haven't noticed any advancements with try/catch. So why would you use it anyway? And yes, I read some stuff about it, but nothing that made me think it would be handy to use it...
 
I haven't noticed any advancements with try/catch. So why would you use it anyway? And yes, I read some stuff about it, but nothing that made me think it would be handy to use it...

What I was going to use it for is if there was say, an error thrown by mysql, the user would not see that error (Which is ugly), rather something saying I have been notified of an error and the error has been logged. However, the error handling class I have made does this for me, so there is no need to use try / catch anymore.
 
Yeah, I made my own way of error logging, which is shorter than try/catch. Thats what I mean, I don't see any advancements in it...
 
Exceptions can be thrown by a childclass and caught at a centralised position. This makes them usefull in very large scale programs: what you do is overwrite PHP's error handler with a function that throws an exception everytime an error occured and use a central exception handling class. Its a nice way to make sure errors are handled correctly.

Of course you can also integrate this behaviour in your error handling function, but than you'd have to manually abort a function when an error occurs: exceptions automatically break any loop they're in and return the error to their parent.

Say for instance I have a database class, and if it can't connect it'll throw an exception (I actually have written a databaseclass with this behaviour :wink:). Now, if I try to create a new database object to execute a query and place try / catch handlers around both the object call as the query I can be certain that if the database object isn't created properly it will also not try to execute the query - something which might for instance save your ass if you use 2 databases and you made a call to the other one previously (since PHP's default behaviour when no database instance is given is to use the last used instance). Now you can of course use statements like if(!$instance) die(), but using the exception handler these become obsolute and errors are handled much more gracefully.

Though truth be told there are only very few places I actually use them, since usually a simple display_error(none) and a global $error variable string work just as well for small projects :icon6:
 
Exceptions can be thrown by a childclass and caught at a centralised position. This makes them usefull in very large scale programs: what you do is overwrite PHP's error handler with a function that throws an exception everytime an error occured and use a central exception handling class. Its a nice way to make sure errors are handled correctly.

Of course you can also integrate this behaviour in your error handling function, but than you'd have to manually abort a function when an error occurs: exceptions automatically break any loop they're in and return the error to their parent.

Say for instance I have a database class, and if it can't connect it'll throw an exception (I actually have written a databaseclass with this behaviour :wink:). Now, if I try to create a new database object to execute a query and place try / catch handlers around both the object call as the query I can be certain that if the database object isn't created properly it will also not try to execute the query - something which might for instance save your ass if you use 2 databases and you made a call to the other one previously (since PHP's default behaviour when no database instance is given is to use the last used instance). Now you can of course use statements like if(!$instance) die(), but using the exception handler these become obsolute and errors are handled much more gracefully.

Though truth be told there are only very few places I actually use them, since usually a simple display_error(none) and a global $error variable string work just as well for small projects :icon6:

Aye, I can see its usefulness in large projects, but for small I will just use this error handler - its rather nice.
 
Back