More About Handling Exceptions
This issue covers:
-More About Handling Exceptions
-Using HTML in Swing Components
These tips were developed using Java 2 SDK, Standard Edition, v 1.4.
This issue of the Core Java Technologies Tech Tips is written
Tutorial Details:
MORE ABOUT HANDLING EXCEPTIONS
The November 24, 2003 tip titled \"Handling Exceptions\" demonstrated some of the things that can go wrong if you ignore or mishandle exceptions. The following tip continues that discussion. Unfortunately, there were two errors in the final code example, FileAccess, of the previous tip. The errors appeared in the version of the tip that was sent to subscribers. The example was corrected after the mailing, and the corrected version appears on the Web site. The following tip actually starts with that faulty code. It shows you how to correct the problems in the code so that it handles exceptions more robustly. This tip also covers some of the components of the exception mechanism that were not introduced in the previous tip.
The following sample program, BadException, highlights three problems. Two of them appeared in the FileAccess sample program in the previous tip. The first of the two (Problem 1 in the code) is the more serious of the problems, and could result in an unanticipated exception being thrown. The second of the problems (Problem 2) is an unnecessary use of the exception mechanism. Problem 3, which did not appear in the previous tip, includes a resource leak and other items that require more explanation of the exception mechanism. Take a moment to consider what is wrong in each case and how you might fix it.
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
public class BadException {
private static String fileName;
public static void main(String[] args) {
FileReader fileReader = null;
try {
fileReader = new FileReader(getFile(args));
} catch (FileNotFoundException e) {
System.out.println(
\"Could not locate a file: \" +
e.getMessage());
}
// problem 1: do not do the following
readFile(fileReader);
}
private static File getFile(String[] args)
throws FileNotFoundException {
// problem 2: do not do the following
try {
fileName = args[0];
} catch (ArrayIndexOutOfBoundsException e) {
throw new FileNotFoundException(
\"correct usage: \" +
\"java FileAccess \");
}
File aFile = new File(fileName);
if (aFile.exists()) {
return aFile;
} else {
throw new FileNotFoundException(
\"There is no file at \" + fileName);
}
}
private static void readFile(
FileReader fileReader) {
// problem 3: items not covered last time
BufferedReader buffReader =
new BufferedReader(fileReader);
String temp = \"\";
try {
System.out.println(
\"== Beginning of the file ==\");
while (
(temp = buffReader.readLine()) != null) {
System.out.println(temp);
}
} catch (IOException e) {
System.out.println(
\"There was a problem reading:\"
+ fileName);
}
System.out.println(
\"== End of the file ==\");
}
}
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: More About Handling Exceptions
View Tutorial: More About Handling Exceptions
Related
Tutorials:
Exceptions in
Java - JavaWorld - July
1998
Exceptions in
Java - JavaWorld - July
1998 |
Designing with
exceptions - JavaWorld - July 1998
Designing with
exceptions - JavaWorld - July 1998 |
Sockets
programming in Java: A tutorial -
JavaWorld
December 1996
Sockets
programming in Java: A tutorial -
JavaWorld
December 1996 |
Validation with Java and XML schema, Part 4 - JavaWorld December 2000
Validation with Java and XML schema, Part 4 - JavaWorld December 2000 |
XML messaging, Part 1 - JavaWorld March 2001
XML messaging, Part 1 - JavaWorld March 2001 |
Robust event
logging with Syslog - JavaWorld April
2001
Robust event
logging with Syslog - JavaWorld April
2001 |
Cache in on faster,
more reliable JSPs - JavaWorld May 2001
Cache in on faster,
more reliable JSPs - JavaWorld May 2001 |
J2SE 1.4
premieres Java's
assertion capabilities, Part 2
J2SE 1.4
premieres Java's
assertion capabilities, Part 2 |
Cache SOAP services on
the client side
Cache SOAP services on
the client side |
Exceptions: Don't
get thrown for a loss
Exceptions: Don't
get thrown for a loss |
Achieve strong performance with threads,
Part 1
Achieve strong performance with threads,
Part 1 |
Reinvented
wheel
Reinvented
wheel |
Java Tip 134: When
catching exceptions, don't cast your net too wide
Java Tip 134: When
catching exceptions, don't cast your net too wide |
Beware the dangers of
generic Exceptions
Beware the dangers of
generic Exceptions |
Java theory and practice: Kill bugs dead
Inspection tools like FindBugs provide a second layer of defense against common coding errors. |
Attribute-Oriented Programming with Java 1.5, Part 2
Peeking Inside the Box: Attribute-Oriented Programming with Java 1.5,Part
In the previous article in this series, "Peeking Inside the Box, Part 1," I introduced the concepts of Attribute-Oriented Programming, Java 1.5 annotations, and bytecode instrume |
Handling Events in JavaServer Faces, Part 2
Here in part two, Hans implements event handling for parts of the sample application discussed in part one.
|
Interoperability with Patterns and Strategies for Document-Based Web Services
In Part 2 of this article, we demonstrate interoperability for document-driven web services with Microsoft .NET (C#) using strategies discussed in Part 1. |
Core Java Interview Questions!
Core Java Interview Questions!
Core Java Interview Questions
Question: What is transient variable?
Answer: Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written |
Introduction to JSP tags JSP Directives
Introduction to JSP tags JSP Directives
INTRODUCTION TO JSP TAGS
I n this lesson we will learn about the various tags available in JSP with suitable examples. In JSP tags can be devided into 4 different types. These are:
Directives
In the |
|
|
|