You are viewing...

When to use Exception Handling?

Updated on December 24, 2011 at the 15th hour
Posted under:

DISCLAIMER: All views are considered my own and you should not draw any conclusions on associates.

This is something programmers have come by: try { // blah } catch( Exception ) { // handle }. The infamous exception handler clause. It is something that can be overused. If I still have code (PHP) that shows my overuse of them, then I’ll post it on github. It might seem better to just never use them, in the context of web programming.

Today, I read a comment on the PHP documentation describing exception handling as a way to handle graceful abortion of your script. That makes sense, of course, but when should we use exceptions? At first, it is a hard question, so just use it to handle every error that would cause your script to function abnormally and gracefully abort. It seems like the best choice, but you’ll figure out the complexity associated and besides who want to write all of that handling code anyway.

You start to get the feel for exception handling, but see that exceptions are not the best way to handle these situations, so you write a Logger function and a error handler to handle these situations.

I suppose I thought there was a better/elegant way of writing if(this){ //then that} else { // blah error }. There are variations on how to do this., but I thought exception handlers were the best way to do it because all I’d have to do when an error occurred was throw an exception! Throwing involves catching and I was not prepared for the complexity that arose from that.

Anyway, what are the best times to use exceptions?

Let’s say you’ve requested some data from a web service, but it returns an error, then that could be handled using an exception because you will not receive the data structure you want.

Let’s say you’ve tried to connect to or requested data from your database, but it failed, good case of using exceptions.

Let’s say one of your functions could not execute what it should have done, is that a good case to use exceptions? I think it depends on what the function does and returns. At that point, it is time to re-evaluate what the function is meant for as the simplest thing for a function to do is one thing in particular.

Failure in setting and getting a value does not warrant an exception. Deleting and creating usually occur from a database query and any failures with that will be handled by your try/catch block associated with the query. If you were opening a file and it failed, then yes you can use an exception handler.

The point being that these are pretty exceptional cases (although you don’t strictly need to use exceptions) and there are not as many exceptional cases as in programming an application for a desktop or a mobile device where memory exceptions are something you have to handle whether it means terminating your application or freeing up memory. In programming a web application where scripting remains dominant, exceptions are very sparse and most of the time your should be using your own default exception handler.

A big mistake in using default exception handlers is exposing sensitive information, obviously if you were on a development server then this poses no risk. This extends to desktop/mobile applications where exposing memory addresses that point to unencrypted information is a or can be big risk.

You just read "When to use Exception Handling?". Please share if you liked it!
You can read more recent posts here.