Friday, September 03, 2010
Java Essentials for Embedded Networked Devices - Part 3: Errors, exceptions and exception handling
Part 3 of an excerpt from the book "Designing Embedded Internet Devices" discusses exceptions and exception handling, including examples.
Brian DeMuth and Dan Eisenreich
[Part 1 of this article discusses how to obtain and set up Java for Windows and Linux PCs. Part 2 reviews classes, objects, and methods, as well as OOP diagrams and inheritance.]
Errors, exceptions, and exception handling
Nothing is worse than software that crashes. Java provides a method for handling software errors through
what is known as exception handling. This section will briefly discuss exceptions and exception handling, including:
• Definition of exceptions and exception handling
• Different types of exceptions
• Exception examples: a specified exception, our very own exception
An exception, simply put, is something going wrong during the course of program execution. Exception handling is the term used to describe how the program will deal with these exceptions.
When an unexpected condition occurs, an exception object is created and the method or class that experienced the error condition is said to throw the exception. The exception object can then be passed throughout the program and handled where it's most appropriate to do so. The basic mechanism in Java for handling these exception objects is the try/catch block. Consider a simple example.
import java.io.*;
// This program will NOT compile
public class CheckedException {
public static void main(String[] args) {
String inputLines;
BufferedReader myFile = new BufferedReader(new FileReader(args[0]));
while ((inputLines=myFile.readLine()) != null) {
System.out.println(inputLines);
}
}
}
The program example above tries to open a text file, read in the text, and print that text to the screen. The code block responsible for doing that work is enclosed in a try/catch block. That indicates that the program will try to perform the operations inside the block, and if it is unable to because of an exception, it will catch the exception and execute the code in the catch code block. If we tried to compile the above program without the try/catch statements, we would receive a compiler error.
C:\> javac CheckedException.Java
CheckedException.Java:5: Exception Java.io.FileNotFoundException must be caught, or it must be declared in the throws clause of this method.
BufferedReader myFile = new BufferedReader(new FileReader(args[0]));
^
CheckedException.Java:6: Exception Java.io.IOException must be caught, or it must be declared in the throws clause of this method.
while ((inputLines=myFile.readLine()) != null) {
^
2 errors
The reason for this is that several of the methods used in the program throw exceptions, which requires provisions to check for, or catch them. The Java API shows which methods throw exceptions and precisely which exceptions they throw.
In our example, close() and readLine() are methods of the BufferedReader class, which both throw an exception called IOException. IOException is a class that extends the Exception class and is superclass to a host of more specific classes such as the FileNotFoundException. The FileReader constructor throws the FileNotFoundException, which can be caught separately, or as an IOException because it's a subclass of that exception. The following revised version compiles without errors.
import java.io.*;
// This one will compile
public class FixedCheckedException {
public static void main(String[] args) {
String inputLines;
try {
BufferedReader myFile = new BufferedReader(new FileReader(args[0]));
while ((inputLines=myFile.readLine()) != null) {
System.out.println(inputLines);
}
} catch(IOException e) {
System.out.println("There was an IOException");
}
}
}
Page 2: Exception types
Page 3: Exceptions (cont.)
Page 4: Catch, specify and the finally keyword
Page 1 2 3 4


