Java Exception Handling

Topics: Exception handling, Java, Subroutine Pages: 29 (7853 words) Published: March 21, 2012
[Page 575]

Part 4: Exception Handling, I/O, and Recursion
The Goddess Chalchihuitlicue, found in the Valley of Mexico, 1300-1500 AD (stone), Aztec / Musée de l'Homme, Paris, France / Bridgeman Art Library

This part introduces the use of exception handling and assertions to make your programs robust and correct, the use of input and output to manage and process a large quantity of binary data, and the use of recursion to write methods for solving inherently recursive problems. [Page 576]

Prerequisites for Part 4
All the chapters after Chapter 17 are designed to minimize dependencies so that they can be reordered flexibly. Chapter 17, "Exceptions and Assertions," can be covered after Chapter 9, "Inheritance and Polymorphism." Chapter 18, "Binary I/O," is usually covered after Chapter 17. The concept of recursion and how to write simple recursive programs in §§19.1–19.3 can be covered after Chapter 6, "Arrays."

Chapter 17 Exceptions and Assertions Chapter 18 Binary I/O Chapter 19 Recursion [Page 577]

Chapter 17. Exceptions and Assertions
Mayan God Shel, Mexico. Photographer: Philip Coblentz. Courtesy Brand X Pictures

To understand exceptions and exception handling (§§17.2–17.3). To distinguish exception types: Error (fatal) vs. Exception (non-fatal), and checked vs. unchecked (§17.3). To declare exceptions in a method header (§17.4.1). To throw exceptions in a method (§17.4.2). To write a try-catch block to handle exceptions (§17.4.3). To explain how an exception is propagated (§17.4.3). To use the finally clause in a try-catch block (§17.5). To know when to use exceptions (§17.6). To rethrow exceptions in a try-catch block (§17.7). To create chained exceptions (§17.8). (Optional) To declare custom exception classes (§17.9). (Optional) To apply assertions to help ensure program correctness (§17.10). [Page 578]

17.1. Introduction
Three categories of errors (syntax errors, runtime errors, and logic errors) were introduced in §2.15, "Programming Errors." Syntax errors arise because the rules of the language have not been followed. They are detected by the compiler. Runtime errors occur while the program is running if the environment detects an operation that is impossible to carry out. Logic errors occur when a program doesn't perform the way it was intended to. In general, syntax errors are easy to find and easy to correct because the compiler indicates where they came from and why they occurred. You can use the debugging techniques introduced in §2.16, "Debugging," to find logic errors. This chapter introduces using exception handling to deal with runtime errors and using assertions to help ensure program correctness.

[Page 578 (continued)]

17.2. Exception-Handling Overview
An exception is a runtime error. A program that does not provide code for catching and handling exceptions will terminate abnormally, and may cause serious problems. For example, if your program attempts to transfer money from a savings account to a checking account, but because of a runtime error is terminated after the money is drawn from the savings account and before the money is deposited in the checking account, the customer will lose money. Exceptions occur for various reasons. The user may enter an invalid input, for example, or the program may attempt to open a file that doesn't exist, or the network connection may hang up, or the program may attempt to access an out-of-bounds array element. You have already been briefly introduced to exceptions in earlier chapters. In Chapter 6, "Arrays," you learned that an ArrayIndexOutOfBoundsException occurs if you access an element past the end of an array. In Chapter 7, "Objects and Classes," you learned that a NullPointerException occurs when you invoke a method on a reference variable with null value. In Chapter 8, "Strings and Text I/O," you learned that a StringIndexOutOfBoundsException occurs when you access a character in a string using an out-of-bound...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Exception Handling Essay
  • Exception Handling in C++ Essay
  • Java Programming Essay
  • Java Sevelets Tutorial Research Paper
  • Java Editor Essay
  • Essay about Spring Java J2Ee
  • Java Essay
  • Java Essay

Become a StudyMode Member

Sign Up - It's Free