<< Chapter < Page Chapter >> Page >

Thus, the requirement to handle an exception that has been thrown progresses up through the call stack until an appropriate handler is found to handle theexception. If no appropriate handler is found, the runtime system and the program terminate.

(If you have ever had a program terminate with a NullPointerException , then you know how program termination works).

According to the jargon, the exception handler that is chosen is said to catch the exception.

Advantages of using exception handling

According to Campione and Walrath, exception handling provides the following advantages over "traditional" error management techniques:

  • Separating Error Handling Code from "Regular" Code
  • Propagating Errors Up the Call Stack
  • Grouping Error Types and Error Differentiation

Separating error handling code from regular code

I don't plan to discuss these advantages in detail. Rather, I will simply refer you to The Java Tutorial and other good books where you can read their discussions. However, I will comment briefly.

Campione and Walrath provide a good illustration where they show how a simple program having about six lines of code get "bloated" into about 29 lines of veryconfusing code through the use of traditional error management techniques. Not only does the program suffer bloat, the logical flow of the original programgets lost in the clutter of the modified program.

They then show how to accomplish the same error management using exception handling. Although the version with exception handling contains about seventeenlines of code, it is orderly and easy to understand. The additional lines of code do not cause the original logic of the program to get lost.

You must still do the hard work

However, the use of exception handling does not spare you from the hard work of detecting, reporting, and handling errors. What it does is provide a means toseparate the details of what to do when something out-of-the-ordinary happens from the normal logical flow of the program code.

Propagating exceptions up the call stack

Sometimes it is desirable to propagate exception handling up the call stack and let the corrective action be taken at a higher level.

For example, you might provide a class with methods that implement a stack . One of the methods of your class might be to pop an element off the stack.

What should your program do if a using program attempts to pop an element off an empty stack? That decision might best be left to the user of your stackclass, and you might simply propagate the notification up to the calling method and let that method take the corrective action.

Grouping exception types

When an exception is thrown, an object of one of the exception classes is passed as a parameter. Objects are instances of classes, and classes fall intoan inheritance hierarchy in Java. Therefore, a natural hierarchy can be created, which causes exceptions to be grouped in logical ways.

For example, going back to the stack example, you might create an exception class that applies to all exceptional conditions associated with an object ofyour stack class. Then you might extend that class into other classes that pertain to specific exceptional conditions, such as push exceptions, pop exceptions, and initialization exceptions.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask