Benny
Benny

Reputation: 8815

Can someone please explain exception handling to me?

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.

Upvotes: 2

Views: 319

Answers (7)

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120937

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.

Upvotes: 2

Jeff B
Jeff B

Reputation: 30099

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.

Upvotes: 1

John Parker
John Parker

Reputation: 54445

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.

Upvotes: 1

Hank Gay
Hank Gay

Reputation: 71959

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

Upvotes: 0

McAden
McAden

Reputation: 13972

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

Upvotes: 6

danben
danben

Reputation: 83250

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

Upvotes: 3

Macha
Macha

Reputation: 14654

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.

Upvotes: 3

Related Questions