Skip to main content
Advertisement

8.1 Exception Handling Overview

Note: This guide is written based on Java 21. For the most up-to-date and comprehensive details, please refer to the official Java Documentation.

While developing a program, encountering various errors is inevitable. Users might input letters where numbers are required, a file might be deleted before it is read, or the network connection may drop. These unpredictable situations are called exceptions.

In Java, Exception Handling is an essential mechanism used to pre-emptively write code for unexpected error situations that occur during program execution. The main goal is to prevent the program from crashing abruptly (abnormal termination) and maintain a normal execution state.

1. Error vs. Exception

Java categorizes problems that can occur during program execution into two broad types.

  1. Error
    • These are severe, irrecoverable problems that paralyze the system, such as running out of memory (OutOfMemoryError) or stack overflow (StackOverflowError).
    • Because they cause the program to crash unavoidably, they are outside the realm of what a developer can handle or salvage at the code level.
  2. Exception
    • These are generally lighter forms of errors caused by user mistakes, environmental factors, or programming logic flaws.
    • Exceptions can be caught and handled at the code level using mechanisms like try-catch, allowing the program to recover.

The exceptions (not errors) are what we need to defensively code against.

2. Hierarchy of Exception Classes

In Java, all errors and exceptions are treated as objects, inheriting from the ultimate ancestor, the java.lang.Object class (specifically via java.lang.Throwable). The exception family tree is split into two main branches:

  • Exception and its subclasses:(Exceptions mainly due to external environments)
    • Exceptions occurring due to external factors, like user input.
    • Examples include an incorrect filename (FileNotFoundException) or passing data in the wrong format (DataFormatException).
    • The Java compiler heavily enforces these. If you don't handle them, the code simply won't compile! These are called Checked Exceptions.
  • RuntimeException and its subclasses:(Exceptions due to programmer mistakes)
    • Exceptions predominantly resulting from a programmer's miscalculation or logic error.
    • Common examples are accessing an array index that doesn't exist (ArrayIndexOutOfBoundsException) or invoking a method on a null reference (NullPointerException).
    • The compiler doesn't force you to handle them explicitly. These are known as Unchecked Exceptions.

3. Why Exception Handling is Crucial

  1. It builds a bullet-proof program that doesn't collapse from hidden bugs waiting to detonate.
  2. Instead of frightening the user with a giant red stack trace when an exception occurs, you can gracefully display a friendly message like, "The server is currently under maintenance."
  3. It allows you to safely record (log) the error details, making it significantly easier to debug and fix issues later.

In the next chapter, we will dive deep into the core syntax used to wrangle exceptions in Java: the try-catch block!

Advertisement