Java Exception Handling Guide

In this complete guide to Exception Handling in Java, we will learn everything about exception handling in Java with examples.

What will we learn?

  1. What Is an Exception?
  2. How Exception Handling Works in Java
  3. Three Kinds of Exceptions
  4. Exception Handling Keywords in Java
  5. Java Exception Hierarchy in Java
  6. The try with resources Statement
  7. How to Create Custom Exceptions
  8. Java Chained Exceptions
  9. Advantages of Java Exceptions
  10. Java Exception Handling Best Practices

1. What Is an Exception?

Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
The term exception is shorthand for the phrase "exceptional event."

2. How Exception Handling Works in Java

This topic is covered in a separate post on How Exception Handling Works in Java

3. Three Kinds of Exceptions

Let's look at the three basic categories of exceptions with examples.
  1. Checked Exception or Compile Time Exception
  2. Error
  3. Un-Checked Exception or Runtime Exception
Read more detail on The Three Kinds of Exceptions in Java

4. Exception Handling Keywords in Java

Java exception handling is managed via five keywords: try, catch, throw, throws, and finally.
Briefly, here is how they work. Program statements that you want to monitor for exceptions are contained within a try block. 
If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java runtime system. 
To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed after a try block completes is put in a finally block.
This is the general form of an exception-handling block:
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed after try block ends
}
Here, ExceptionType is the type of exception that has occurred. The remainder of this chapter describes how to apply this framework.
All exception handling keywords are nicely explained on Exception Handling Keywords in Java article.

5. Java Exception Hierarchy in Java

The objects that inherit from the Throwable class include direct descendants (objects that inherit directly from the Throwable class) and indirect descendants (objects that inherit from children or grandchildren of the Throwable class). 
All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the exception class hierarchy. Throwable are two subclasses that partition exceptions into two distinct branches that are:
  1. Error Class
  2. Exception Class

Read more about Exceptions Hierarchy on Exceptions Hierarchy in Java

6. The try with resources Statement

The try-with-resources statement is a try statement that declares one or more resources.
Let's learn below topics about try-with-resources on The try-with-resources Statement with Examples
  • What is a try-with-resources statement?
  • Overview of java.lang.AutoCloseable Interface
  • Working on a try-with-resources statement with BufferedReader Example
  • Resource closing using finally block(Prior to Java SE 7)
  • Declare one or more resources in a try-with-resources statement
  • Custom Resource object close examples
  • try-with-resources Statement with File IO Examples

7. How to Create Custom Exceptions

Although Java’s built-in exceptions handle most common errors, you will probably want to create your own exception types to handle situations specific to your applications. This is quite easy to do: just define a subclass of Exception (which is, of course, a subclass of Throwable).
Let's learn below topics about creating our own exception on Guide to Create Custom Exceptions
  • Steps to create custom exceptions
  • How to create checked custom exceptions with examples?
  • How to create an unchecked custom exception with examples?
  • More examples

8. Java Chained Exceptions

The chained exception feature allows you to associate another exception with an exception. This second exception describes the cause of the first exception.
Read more in detail with examples on Java Chained Exceptions

9. Advantages of Java Exceptions

  • Advantage 1: Separating Error-Handling Code from "Regular" Code
  • Advantage 2: Propagating Errors Up the Call Stack
  • Advantage 3: Grouping and Differentiating Error Types
Let's understand each advantage of Exceptions on a separate article on Advantages of Java Exceptions

10. Java Exception Handling Best Practices

  1. Clean up Resources in a Finally Block or Use a Try-With-Resource Statement
  2. Throw Specific Exception
  3. Do not catch the Exception class rather catch specific subclasses
  4. Never catch a Throwable class
  5. Always correctly wrap the exceptions in custom exceptions so that stack trace is not lost
  6. Catch the most specific exception first
  7. Don’t ignore exceptions rather log the exceptions
  8. Never throw any exception from finally block
  9. Don’t use printStackTrace() statement or similar methods
  10. Use finally blocks instead of catch blocks if you are not going to handle the exception
  11. Validate user input to catch adverse conditions very early in request processing
  12. Throw Exceptions With Descriptive Messages
Learn each exception handling best practices with examples on Java Exception Handling Best Practices

Exception Handling Related Posts

Comments