Actually, one would do it because what should be happening is that in the various methods, you should have other try/catch constructs wherever you expect that there MIGHT be an error.
In those cases, you will catch the exception and return something meaningful to the caller. For example, in your .UpdateCart() method:
Code:
try
{
//some code here
}
catch(SqlException exc)
{
throw new ApplicationException("Update cart had a SQL error!", exc);
}
catch(DivideByZeroException exc)
{
throw new ApplicationException("Update cart tried to divide by zero!", exc);
}
So you see, you've caught it, added some meaningful information to it, and then bubbled it on up the chain. It will then be caught by the main try/catch in the caller, where you can do something with it.
System exceptions will just be thrown. Again, caught higher up the chain.
Notice that I included the original exception with my new one. That's very important also.
Take it another step. At a very basic level, exceptions are classed into Application Exceptions and System Exceptions. Application exceptions are exceptions that your application will throw, and System exceptions are thrown by the framework. Whenever you expect that there might be an error, you should catch those errors, new up an application exception (including the original exception) and then throw it.
In this way, you can construct the main try/catch like this:
Code:
try
{
objCart.UpdateCart();
objCart.DisplayCart();
}//try
catch(ApplicationException exc)
{
lblException.Text = "We had an application exception. Details<br>: " + exc.Message;
}//catch
catch(Exception exc)
{
lblException.Text = "We had a System Exception! Man, we didn't expect that! Details<br>: " + exc.Message;
}//catch
I should also say that you can even create your own exceptions. MissingCustomerException, for example. You can derive a class from SystemException, add your own information, call it what you will, and then trap those separately, as well. For example, if a MissingCustomerException is thrown, maybe you'll redirect your visitor to a page where they can sign up for a new account.
So as you can see, with structured exception handling, the possibilities are limitless as to how you can handle your errors. It's very elegant, and if you spend some time setting up an error handling framework, you'll really have some meaningful information to help you not only track down any bugs that might exist in your software, but give your users a much richer experience.
Be sure to check out Google on this subject for lots and lots of examples. I've really only scratched the surface here.

paul
The answer to getting answered -- faq855-2992