|
HANDLING UNCAUGHT EXCEPTIONS
If you've done much programming in the JavaTM programming language, you've probably encountered applications that terminate abnormally with an uncaught exception
|
|
What's an Exception and Why Do I Care?
The Java language uses exceptions to provide error-handling capabilities for its programs. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
|
|
Your First Encounter with Java Exceptions
The following error message is one of two similar error messages you will see if you try to compile the class InputFile, because the InputFile class contains calls to methods that throw exceptions when an error occurs.
|
|
Java's Catch or Specify Requirement
As stated previously, Java requires that a method either catch or specify all checked exceptions that can be thrown within the scope of the method. This requirement has several components that need further description: "catch", "specify," "checked exceptions," and "exceptions that can be thrown within the scope of the method."
|
|
Dealing with Exceptions
Before you can catch an exception, some Java code somewhere must throw one. Any Java code can throw an exception: your code, code from a package written by someone else (such as the packages that come with the Java development environment), or the Java runtime system. Regardless of who (or what) throws the exception, it's always thrown with the Java throw statement.
|
|
Throwing Exceptions
The Java runtime system and many classes from Java packages throw exceptions under some circumstances by using the throw statement. You can use the same mechanism to throw exceptions in your Java programs. This section shows you how to throw exceptions from your Java code.
|
|
Runtime Exceptions--The Controversy
Because the Java language does not require methods to catch or specify runtime exceptions, it's tempting for programmers to write code that throws only runtime exceptions or to make all of their exception subclasses inherit from RuntimeException. Both of these programming shortcuts allow programmers to write Java code without bothering with all of the nagging errors from the compiler and without bothering to specify or catch any exceptions. While this may seem convenient to the programmer, it sidesteps the intent of Java's catch or specify requirement and can cause problems for the programmers using your classes.
|
|
Exception-Handling Strategies
The judicious and proper use of the Java exception-handling mechanism can pay rich dividends by delivering quality code that works. In order to use the powerful error-handling features of Java, users must understand key issues that impact its design and implementation. This article provides a blueprint for developing a clear and consistent strategy for exception handling
|
|
Designing with exceptions
This installment of the Design Techniques column discusses design guidelines that pertain to exceptions. It focuses primarily on how to decide when to use exceptions, and gives several examples from the Java API that illustrate appropriate uses of exceptions. In addition, the article provides some general guidelines that can help you use exceptions in those situations where you've decided they are appropriate.
|
|
Exceptions to exceptions
Java performance enthusiasts Jack Shirazi and Kirk Pepperdine, Director and CTO of JavaPerformanceTuning.com, follow performance discussions all over the Internet to see what's troubling developers. In this month's stop at the JavaRanch, they counter the campfire stories about exceptions with a detailed look at the story behind the story.
|
|
Build a better exception-handling framework
Enterprise applications are often built with little attention given to exception handling, which can result in over-reliance on low-level exceptions such as java.rmi.RemoteException and javax.naming.NamingException. In this installment of EJB Best Practices, Brett McLaughlin explains why a little attention goes a long way when it comes to exception handling, and shows you two simple techniques that will set you on the path to building more robust and useful exception handling frameworks.
|
|
Java's Catch or Specify Requirement
As stated previously, Java requires that a method either catch or specify all checked exceptions that can be thrown within the scope of the method. This requirement has several components that need further description: "catch", "specify," "checked exceptions," and "exceptions that can be thrown within the scope of the method."
|
|
Your First Encounter with Java Exceptions
The following error message is one of two similar error messages you will see if you try to compile the class InputFile, because the InputFile class contains calls to methods that throw exceptions when an error occurs:
|
|
Dealing with Exceptions
Your First Encounter with Java Exceptions briefly described how you were (probably) first introduced to Java exceptions: a compiler error complaining that you must either catch or specify exceptions. Then, Java's Catch or Specify Requirement discussed what exactly the error message means and why the Java language designers decided to make this requirement. We're now going to show you both how to catch an exception and how to specify one.
|
|
The ListOfNumbers Example
The following example defines and implements a class named ListOfNumbers. The ListOfNumbers class calls two methods from classes in the Java packages that can throw exceptions.
|
|
Specifying the Exceptions Thrown by a Method
The previous section showed you how to write an exception handler for the writeList method in the ListOfNumbers class. Sometimes, it's appropriate for your code to catch exceptions that can occur within it. In other cases, however, it's better to let a method further up the call stack handle the exception.
|
|
The try Block
The first step in constructing an exception handler is to enclose the statements that might throw an exception within a try block.
|
|
The catch Block(s)
As you learned on the previous page, the try statement defines the scope of its associated exception handlers.
|
|
The finally Block
The final step in setting up an exception handler is providing a mechanism for cleaning up the state of the method before (possibly) allowing control to be passed to a different part of the program. You do this by enclosing the cleanup code within a finally block. As you learned on the previous page, the try statement defines the scope of its associated exception handlers.
|
|
Putting It All Together
The previous sections describe how to construct the try, catch, and finally code blocks for the writeList example. Now, let's walk through the code and investigate what happens during three scenarios.
|
|
Beware the dangers of generic Exceptions
Java provides a rich exception-handling framework, but many programmers find it easier to ignore that richness and simply use generic Exceptions. This article explores the risks of throwing, catching, and ignoring generic Exceptions and suggests best practices for dealing with complex exception handling in large, complex software projects.
|
|
Exceptions to the programming rules, Part 1
It provides a glossary of terms specific to that article, tips and cautions, new homework, solutions to last month's homework, and Jeff Friesen's answers to questions from your fellow students.
|
|
Exceptions to the programming rules, Part 2
Welcome to the Java 101 study guide. This guide complements "Exceptions to the Programming Rules, Part 2." It provides a glossary of terms specific to that article, tips and cautions, new homework, solutions to last month's homework, and Jeff Friesen's answers to questions from your fellow students.
|
|
Exceptions in Java: Nothing exceptional about them
The judicious and proper use of the Java exception-handling mechanism can pay rich dividends by delivering quality code that works. In order to use the powerful error-handling features of Java, users must understand key issues that impact its design and implementation. This article provides a blueprint for developing a clear and consistent strategy for exception handling
|
|
Exceptions in Java
For those of you who need a refresher on exceptions, this cover story companion piece is a valuable tutorial on the nuts and bolts of what exceptions are and how they work in the Java language and virtual machine. Bill Venners describes in detail exception mechanisms built into the Java programming language.
|
|
Exceptional practices, Part 1
Exceptions provide a powerful mechanism for handling error conditions in Java programs. However, many developers don't spend enough time thinking about exceptions during the design process; instead, they let the development process drive the program's use of exceptions. In this series, Brian Goetz reviews some best practices for using exceptions effectively. In Part 1, he offers guidelines on properly incorporating error handling into classes at design time
|
|
Exceptional practices, Part 2
Exceptions provide a powerful mechanism for handling error conditions in Java programs. However, many developers don't spend enough time thinking about exceptions during the design process; instead, they let the development process drive the program's exception use. In this series, Brian Goetz reviews some best practices for using exceptions effectively. Part 2 covers a specific technique -- exception chaining -- that you can use to preserve important debugging information. Exception chaining is so useful and widely applicable that it has been added to the Throwable class in JDK 1.4
|
|
Exceptions: Don't get thrown for a loss
Please explain the difference between checked exceptions and runtime exceptions? When would I throw them, and when would I catch them?
|