Skip to main content Accessibility help
Internet Explorer 11 is being discontinued by Microsoft in August 2021. If you have difficulties viewing the site on Internet Explorer 11 we recommend using a different browser such as Microsoft Edge, Google Chrome, Apple Safari or Mozilla Firefox.

Chapter 6: Exception Handling

Chapter 6: Exception Handling

pp. 360-424

Authors

, Indian Institute of Technology, Kharagpur, , Indian Institute of Technology, Kharagpur
  • Add bookmark
  • Cite
  • Share

Extract

There is a need for software developers to build reliable and robust software. To make such a software, the 80:20 rule has to be followed, that is, 80 percent of the effort should go into checking and handling errors, and only 20 percent in writing the software. Error detection and error handling remain an important issue in software development. Java arms developers with an elegant mechanism for handling errors that produces efficient and organized error-handling code. This mechanism is a unique feature in Java and called exception handling. Exception handling allows developers to detect errors easily without writing special code to test return values. Even better, it lets the programmer keep exception-handling code cleanly separated from the exception-generating code. It also lets the programmer use the same exception-handling code to deal with a range of possible exceptions.

This chapter devotes to cover the fascinating concept of exception handling in Java.

Introduction

What is the issue? Programmers in any language endeavor to write bug-free programs, programs that never crash, programs that can handle any situation efficiently, and that can recover from unusual situations without causing the users any undue inconvenience. Good intentions aside, programs that cover all these points don’t exist. In real life, errors occur, either because the programmer didn’t anticipate every situation the code would get into (or did not have the time to test the programs enough), or because of situations outside the programmer’s control like bad data from users, corrupt files that do not have the right data in them, network connections that do not connect, hardware devices that do not respond, to name a few.

Requirement In Java, unusual events that may cause a program to fail are called exceptions. The dictionary meaning of “exception” is “an abnormal situation.” What will happen to the program if an abnormal situation occurs? For example, what result will your calculations return when divide x by y when y = 0? Or, if you want to store a value in the array in its 100th location while the size of the array is 50? In practice, in such a situation, your execution will be suspended immediately, for which you are not really prepared.

About the book

Access options

Review the options below to login to check your access.

Purchase options

eTextbook
US$99.99
Paperback
US$99.99

Have an access code?

To redeem an access code, please log in with your personal login.

If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.

Also available to purchase from these educational ebook suppliers