views:

196

answers:

7

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.

+3  A: 

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.

Macha
+2  A: 

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.)

danben
"so that your application can handle it gracefully" - he just said he doesn't know what *"handle"* means...
BlueRaja - Danny Pflughoeft
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
+6  A: 

"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.

McAden
A: 

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

Hank Gay
+1  A: 

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.

middaparka
+1  A: 

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.

Jeff B
+2  A: 

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.

klausbyskov