100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Exceptions in Java

Understand what exceptions are in Java, how the Throwable hierarchy is organized, and why exception handling keeps programs robust.

Exception HandlingBeginner8 min readJul 7, 2026
Analogies

1. Introduction

An exception in Java is an event that disrupts the normal flow of a program's instructions during execution. When a method encounters an unexpected situation — such as dividing by zero, accessing an invalid array index, or trying to open a file that doesn't exist — it creates an exception object and hands it off to the runtime system, a process called throwing an exception.

🏏

Cricket analogy: Just as sudden rain during an ODI forces umpires to invoke Duckworth-Lewis rules, a Java exception like ArithmeticException from dividing by zero disrupts normal execution and gets thrown to the runtime.

Java's exception handling mechanism lets you separate error-handling code from regular business logic, making programs easier to read and far more resilient. Instead of a program crashing outright, well-placed exception handling lets it fail gracefully, log useful diagnostic information, or recover and continue.

🏏

Cricket analogy: Separating exception handling from business logic is like a team having a dedicated rain-delay protocol separate from match strategy, so a sudden downpour doesn't crash the whole game plan, just triggers a graceful pause.

2. Syntax

text
// Throwable hierarchy (simplified)
java.lang.Object
   java.lang.Throwable
         java.lang.Error                  (unrecoverable, e.g. OutOfMemoryError)
         java.lang.Exception              (recoverable)
               RuntimeException            (unchecked)
                    NullPointerException
                    ArrayIndexOutOfBoundsException
                    ArithmeticException
               IOException, SQLException...(checked)

3. Explanation

Every exception in Java is an object whose class ultimately derives from Throwable. Throwable has two direct subclasses: Error and Exception. Errors represent serious problems that a typical application should not try to catch — things like OutOfMemoryError or StackOverflowError, which usually indicate the JVM itself is in trouble. Exception represents conditions that a well-written application should anticipate and, where possible, recover from.

🏏

Cricket analogy: Throwable is like the umbrella category of match stoppages, splitting into Error (a ground floodlight failure that ends play, like StackOverflowError) and Exception (a rain delay a team can recover from, like a checked IOException).

Exception itself splits into two broad categories: checked exceptions (any subclass of Exception that is not a RuntimeException, such as IOException or SQLException) and unchecked exceptions (RuntimeException and its subclasses, such as NullPointerException or ArithmeticException). Checked exceptions are verified by the compiler — the compiler forces you to either catch them or declare them with throws. Unchecked exceptions are not checked at compile time; they typically signal programming mistakes rather than external failures.

🏏

Cricket analogy: Checked exceptions are like mandatory pre-match pitch inspections a team must plan for (like IOException), while unchecked exceptions are like a batsman's unforced error (like NullPointerException) signaling a mistake, not an external constraint.

Exam trap: Error and RuntimeException are NOT the same thing. Error is a sibling of Exception (both extend Throwable directly), while RuntimeException is a subclass of Exception. A common mistake is assuming Errors are 'unchecked exceptions' — technically Errors aren't Exceptions at all, though they are unchecked in the sense the compiler doesn't force handling them.

4. Example

java
public class ExceptionDemo {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30};
        try {
            System.out.println("Accessing index 5...");
            System.out.println(numbers[5]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Caught exception: " + e.getClass().getSimpleName());
            System.out.println("Message: " + e.getMessage());
        }
        System.out.println("Program continues normally.");
    }
}

5. Output

text
Accessing index 5...
Caught exception: ArrayIndexOutOfBoundsException
Message: Index 5 out of bounds for length 3
Program continues normally.

6. Key Takeaways

  • An exception is an object representing an abnormal event that disrupts normal program flow.
  • All exceptions derive from java.lang.Throwable, which has two direct subclasses: Error and Exception.
  • Errors (e.g. OutOfMemoryError, StackOverflowError) are unrecoverable and generally should not be caught.
  • Exception splits into checked exceptions (compiler-enforced, e.g. IOException) and unchecked RuntimeExceptions (e.g. NullPointerException).
  • Proper exception handling lets programs fail gracefully instead of crashing abruptly.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#ExceptionsInJava#Exceptions#Syntax#Explanation#Example#ErrorHandling#StudyNotes#SkillVeris