Java Exception Handling
Java exception handling enables your Java applications to handle errors sensibly. Exception handling is a very important yet often neglected aspect of writing robust Java applications or components. When an error occurs in a Java program it usually results in an exception being thrown. How you throw, catch and handle these exception matters. There are several different ways to do so. Not all are equally efficient and fail safe.
This Java Exception Handling tutorial trail (set of articles) digs deeper into exception handling in Java. The trail covers various do's and dont's of Java exception handling. It also covers a few techniques for efficient and less error prone exception handling. Hopefully you can benefit from some of these texts.
The versions of Java used in this tutorial are Java 6 and Java 7, though most of the techniques here work already from Java 5 and forward. Some even from Java 4.
Below follows a quick introduction to what you can learn in this Java exception handling trail.
Java Exception Handling Concepts
Here is a list of Java exception handling concepts:
- Basic try-catch-finally
- Try With Resources
- Catching Multiple Exceptions
- Exception Hierarchies
- Checked or Unchecked Exceptions?
- Exception Wrapping
- Fail Safe Exception Handling
- Pluggable Exception Handlers
- Where to Log Exceptions?
- Validation: Throw Exceptions Early
- Validation: Throw Exception or Return False
- Exception Handling Templates
- Exception Enrichment
- Execution Context
I will explain these concepts in a bit more detail below, and also in their own articles (click the links above).
Basic Java Exception Handling
The first 2 texts on Java exception handling covers the basics of Java's exception throwing and catching mechanism, and exception hierarchies. These texts are:
Checked vs. Unchecked Exceptions in Java
Java is one of the few languages to support both checked and unchecked exceptions. In the text Checked or Unchecked Exceptions? I discuss the difference between these two types of exceptions. I also end up recommending unchecked exceptions over checked exceptions.
General Java Exception Handling Advice
The middle 6 texts in this Java exception handling trail covers issues like "fail safe exception" handling, "where to log exceptions", advice on throwing exceptions during validation etc. In other words, various advice on what to remember when designing your application exception throwing and handling.
Advanced Exception Handling in Java
The two texts Exception Handling Templates and Exception Enrichment discusses two somewhat more advanced exception handling techniques that can clean up your code a bit. Exception templates moves all the "try-catch" code to one or more reusable exception handling templates. Exception enrichment rids you of having tons of different exceptions in your exception hierarchy. It also helps you avoid long exception stack traces, and get truly unique error codes for each error in your application.
Exception Handling Strategies
I have written a longer tutorial on how to put the techniques described in this tutorial into a coherent exception handling strategy. It is available in its own trail at Exception Handling Strategies