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
// 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
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
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
1. Which class is the root of the Java exception hierarchy?
2. Which of these is a direct subclass of Throwable, sibling to Exception?
3. Which of the following best describes OutOfMemoryError?
4. What does 'throwing an exception' mean?
5. Why does Java separate exceptions into checked and unchecked categories?
Was this page helpful?
You May Also Like
try-catch in Java
Learn how to use try, catch, multi-catch, and try-with-resources blocks in Java to handle exceptions cleanly.
Checked vs Unchecked Exceptions in Java
Compare checked and unchecked exceptions in Java, including compiler enforcement, typical use cases, and common exam pitfalls.
finally Block in Java
Learn how the finally block guarantees cleanup code execution in Java, even when exceptions or return statements are involved.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics