Fatal Error: Uncaught Excep….. – Introduction to Exceptions

I know that all of you have probably came to a website, or maybe your program, and have seen one of those annoying PHP errors. If you haven’t, it looks something like this:

Parse error: parse error in H:\cha_frame\public_html\index.php on line 6

That error was one that I manually created on my home computer, but all of them pretty much look a like. A blank page, except for some text at the top, usually describing a page error in some weird lingo that could only make sense to a very enthusiastic code monkey.

But I’ll give you a hint: Most code monkeys don’t really know what they mean!. The errors that PHP makes are very ‘machine’-like, and it would probably take you a minute or two to figure out exactly what they mean. These are example of bad errors, ones that don’t clean up gracefully, meaning that if they are there, any user that sees them is just going to sit and wonder what they did wrong, or worse, think your site is broken and leave. The fact is that these errors are very hard to get rid of, and we’ll discuss in another post about how to get rid of them. In this post, we are going to talk about YOUR errors.

Let’s say that you want to create an application. Of course the application is going to have errors. You could use PHP’s built in user_error(“Error Message Here”) function to report errors, and they would show up something like above. However, those error messages are ugly, and we want to create pretty error messages so that our user’s don’t relive the horrible experience of the PHP error message. This is where Exceptions come in handy.

Think of exception like a football. At the beginning of a script, PHP hands the code a football, and the script starts running. It executes all of the messages, but eventually it might get to a snag. Let’s say we are getting information about a user, and it doesn’t exist:

$user = get_user();

We can use an Exception to throw the football, and then catch it somewhere else to handle the error message.

$user = get_user();
if($user == false)
{
     throw new Exception("No Users Exist");
}

What this does, is it tells PHP that the code being ran right now has come upon something unexpected, and doesn’t know what to do. So, it throws the football, and expects something to catch it. But how do we catch it you may ask? Using try and catch blocks, similar to if and else blocks, we can catch the statement. Take the following code:

try{

	$user = get_user();
	if($user == false)
	{
		 throw new Exception("No Users Exist");
	}

}
catch(Exception $e)
{
	die($e->getMessage());
}

What this does is pretty straight forward. We put a try block around our code that can throw the exception. Then, we told PHP that we can catch an Exception, and to assign the object it creates to $e. Then, $e is an object that we can use a function inside called getMessage to grab the message that was sent.

Check out PHP’s Exception Documentation for a more thorough look through.

This entry was posted in Featured, Headline, RTFM. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>