Take the 2-minute tour ×

I think this is a dumb question but I hear and see the term exception handling a lot. I have used try/catch, but i am still wondering what on earth 'handling' means. Can anyone kindly give some example that we can say the exception is actually 'handled'?

sorry for poor English, hope i made myself clear.

share|improve this question
It is a common English word. From Merriem-Webster Dictionary: "to act on or perform a required action with regard to." To say a problem has been "handled" means it has been dealt with, resolved, completed. –  BlueRaja - Danny Pflughoeft Jan 21 '10 at 19:16
@BlueRaja, I know the meaning of the English word 'handling'. –  Benny Jan 21 '10 at 19:19

7 Answers 7

up vote 3 down vote accepted

It means catching an Exception and performing some logic based on its type, so that your application can handle it gracefully rather than abruptly shutting down.

Here is an example (albeit a contrived one) in Java:

public int arrayRetrieve(int[] a, int index) {
   return a[index]; 

Given this function, there is no guarantee that index will be a valid position in a. In Java, this will throw an ArrayOutOfBoundsException.

Code that calls arrayRetrieve needs to be aware of this possibility, and handle this case accordingly:

int num = 0;
try {
    num = arrayRetrieve(someArray, 77);
}  catch (ArrayOutOfBoundsException e) {
    // Set num to a default value, or log an error, or however you want to handle this case

If the ArrayOutOfBoundsException were not caught, this would cause the program to crash instead.

(One reason I said this example is contrived is because Java in particular has two kinds of Exception - the kind you have to catch explicitly, and the kind you don't. ArrayOutOfBoundsException is an example of the latter.)

share|improve this answer
"so that your application can handle it gracefully" - he just said he doesn't know what "handle" means... –  BlueRaja - Danny Pflughoeft Jan 21 '10 at 19:18
Yep, that's why I provided an example - you know, the 90% of the post that came after that line you quoted. Proactive thinking and all that. –  danben Jan 21 '10 at 19:24

"Handling" basically consists of dealing with an error gracefully - rather than making assumptions and just letting your program blow up.

This could consist of logging and moving on, swallowing (hiding it and pretending it never happened), or displaying an error and cancelling the current operation, or it could actually consist of closing the application. It all depends on the application, and what the exception is.

share|improve this answer

Exception handling is when you catch an exception and deal with it. Dealing with it might be recovering from it, prompting an error message, or just using sample data - what's appropriate depends on your application. Using sample data might be fine in a game, where one blue pixel won't do any harm, while it's a bad idea in medical software.

It is contrasted with exception swallowing, which is catching the exception and not doing anything with it.

share|improve this answer

Exception handling refers to the action of dealing with an exceptional event in your program in such a way that the program does not crash, but instead keeps on running in a meaningful way. You say that you have used try/catch, well then whatever you do in the catch-block is the actual exception handling.

share|improve this answer

At a simple level, 'handling' just means 'dealing with'.

For example, you might...

  • Log the error data to a file or database
  • Display an error message to the user
  • Terminate the execution of the program

..depending on the severity of the error/sort of application you're developing.

share|improve this answer

It is called exception handling, because an exception is not always a terminating condition.

When you get an exception, you can "handle" that exception, by correcting whatever caused the exception and proceeding.

For instance, you may get a "divide by zero" exception. If you have an exception handler, you can catch the exception, and either fix the offending data, or cause an "invalid data" message, rather than your application completely dying.

share|improve this answer

The code in the catch block (or whatever your language uses) is "handling" the exception, or at least it's supposed to.

share|improve this answer

Your Answer


