Advantages of Exceptions

Advantage 1: Separating Error-Handling Code from “Regular” Code

Traditional error management needs error detection, reporting and returning. which often makes the logic flow of the code hard to follow.

Advantage 2: Propagating Errors Up the Call Stack

Traditional error-notification techniques force all the method in the call stack to propagate the error codes returned by readFile up the call stack until the error codes finally reach the only method that is interested in them.

The Java runtime environment searches backward through the call stack to find any methods that are interested in handling a particular exception. Only the methods that care about errors have to worry about detecting errors.

Advantage 3: Grouping and Differentiating Error Types

You could set up an exception handler that handles any Exception with the handler here.

// A (too) general exception handler
catch (Exception e) {
    ...
}

The Exception class is close to the top of the Throwable class hierarchy. Therefore, this handler will catch many other exceptions in addition to those that the handler is intended to catch. You may want to handle exceptions this way if all you want your program to do, for example, is print out an error message for the user and then exit.

In most situations, however, you want exception handlers to be as specific as possible. The reason is that the first thing a handler must do is determine what type of exception occurred before it can decide on the best recovery strategy. In effect, by not catching specific errors, the handler must accommodate any possibility. Exception handlers that are too general can make code more error-prone by catching and handling exceptions that weren’t anticipated by the programmer and for which the handler was not intended.

As noted, you can create groups of exceptions and handle exceptions in a general fashion, or you can use the specific exception type to differentiate exceptions and handle exceptions in an exact fashion.

cited from http://docs.oracle.com/javase/tutorial/essential/exceptions/advantages.html

Related Reference:

exceptions in C++    http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.6

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s