Why try catch exception?

Hi can someone please briefly explain why I should use try catch exceptions?

I thought I test it so I wrote this but it doesnt make sense to me.
Thanks

try
{
	if(is_array($results))
	{
		foreach($results as $val=>$row)
		{
			echo html_escape::escape($row->email)."<br />";
		}
	}	
	else
	{
		throw new Exception ("No results found");
	}
}
catch (Exception $e)
{
	echo $e->getMessage();
}

http://www.php.net//manual/en/language.exceptions.php

I know the manual. I need good tutorials. php.net is just a reference, its not tutorial.

Thanks

The specific example you posted doesn’t make sense to me either. I don’t think that’s a good use of exceptions.

You should only catch an exception if your program needs to handle said exception in a particular way, or can perform an alternate task when given exception occurs.

In short, you will see a lot of applications utilizing exceptions in their third party assemblies. So I may have an assembly that is used for validating bank accounts. If the given input is missing that is needed to run the validation, it may throw an exception stating the arguments are missing, it may throw a different exception for invalid input.

The calling application may catch the latter exception (invalid input) but choose not to catch the one for missing arguments, because it could ask the user to correct its entry mistake, but it can’t recover from missing arguments, as that’s an actual programming issue (the programmer failed to pass the arguments in).

Perhaps but I have found the examples and user comments sections particularly useful and would often use them as tutorials.

As cpradio said, when you need particularly handling of errors (rather than just letting it output to the browser), a try-catch block would be useful. It is good practice in general to plan ahead for any potential errors (even if you think your code is robust) else it could be a security flaw.

Hi;

Can someone please show try catch exception in my example?

Thanks

Paste the HTTP Request code from you example image and I’'ll add some try catch stuff.

In that code, I don’t see much opportunity for fine-grained exception handling, but there’s an opportunity for catch-all exception handling.

The first code your posted is probably not a good example on why you should use Exceptions but just show HOW exception works.

try
{
    if(is_array($results))
    {
        foreach($results as $val=>$row)
        {
            echo html_escape::escape($row->email)."<br />";
        }
    }    
    else
    {
        throw new Exception ("No results found");
    }
}
catch (Exception $e) 
{
    echo $e->getMessage();
}  

Sometimes, beginners discovering Exceptions have a tendency to use them in their normal application flow… Like validating user input. Invalid email? Lets throw an exception! Invalid username? Lets throw an exception and catch the exception and display an error message (what it seems like in your first example).

Exceptions shouldn’t be used to manage your inner errors in your code. Exceptions are for really exceptional abnormal unwanted exceptions. And catching “really exceptional abnormal unwanted exceptions” should be done only when you can do something about it. Lets say that you use a library that display your last tweet on your website.

If the library throws a “TwitterNotAvailableException”, it would be a good idea to catch it and just hide the twitter box displaying your last tweet.
If you don’t catch it, users going on your site when Twitter isn’t available will (should) get a 500 page.