Jenkov.com
Tutorials About




Java Exception Handling


Last updated: 2014-06-23

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 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.

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

Java Exception Handling - Kindle Book

I have written a book about Java exception handling for the developers who are interested in getting into greater detail about Java exception handling, especially about exception handling in Java applications, meaning in a complete application perspective.

Go to the Java Exception Handling book page.


Connect with me: Newsletter - Get all my free tips!
     



Close TOC
This website uses cookies to improve the user experience and gather statistics. Our advertisers use cookies too (3rd party cookies), to provide more relevant ads. Continued use of this website implies that you accept the use of cookies on this website. We do not share our cookies with our advertisers, and our advertisers do not share cookies with us.
OK