Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Exceptions in Java: Nothing exceptional about them

Exception handling in Java from top to bottom

  • Print
  • Feedback

Page 5 of 5

So, for the purposes of our comparison, what needs to happen in Case 1? If a NoPassengerFoundException throws while executing the searchPassengerFlightRecord() method (the top stack frame), further execution of code halts and the Java VM receives control to implement Java's exception-handling mechanism. The Java VM then searches in the current method for a catch clause having NoPassengerFoundException. If it doesn't find such a clause, then the Java VM pops the stack frame for the current method, and the calling method becomes the current method. The Java VM again searches in the current method -- earlier the calling method -- for a suitable catch clause. If the Java VM still doesn't find a suitable catch clause, it pops the current stack frame and then searches the next stack frame for a suitable catch clause, and so on, until it finally finds an appropriate one. Or, if the thread is the last nondaemon thread, then the application terminates with the stack trace printing on the console. In short, expending the Java VM to handle a thrown exception requires more effort, i.e., abrupt method completion is significantly more expensive (performance-wise) than a normal method completion.

Conclusion

It is important to have a well thought-out and consistent exception-handling strategy for the sake of efficiency and good programming practice. Exception handling should be considered not as an add-on but as an integral part of the development process. The power of exceptions provides a chassis on which to develop applications that are robust and dependable by design, rather than by accident.

About the author

Gaurav Pal is a consultant with RDA. He has a BS in computer science with more than six years of software development experience, including over three years in Java. He is a Sun-certified Java 2 developer and Java 1.1 programmer. Gaurav has written extensively for publications like IEEE Potentials and Computer Communications UK and maintains his own Website, JavaPal.com. Sonal Bansal is currently in the computer science program at Loyola College, India. He has almost five years of programming experience, having completed work for notable organizations like the Indian Railway Institute of Electrical Engineering (IRIEEN) and Electrical Department, Southern Railway. He has become a devout Java'er after rummaging through C and other languages.
  • Print
  • Feedback

Resources