Errors vs Exceptions, Exception Hierarchy, Checked vs Unchecked & Default Handler
1. Topic Introduction
Even cleanly compiled Java programs face unexpected runtime troubles—dividing by zero, reading a missing file, accessing an invalid array slot, or losing a database connection. Without Exception Handling, the JVM abruptly crashes the entire application. Understanding Java's Throwable class hierarchy and the vital distinction between Checked and Unchecked exceptions is top-priority exam territory.
Exception) vs Engine Falling Off (Error)During a flight, bad weather (IOException = Checked Exception) is predictable—aviation rules force pilots at takeoff (compile time) to file an emergency landing plan (try-catch or throws). Pilot miscalculations (ArithmeticException = Unchecked RuntimeException) happen mid-air due to logic bugs—the compiler doesn't force checks, you should simply fly better! Meanwhile, a wing snapping off (StackOverflowError / OutOfMemoryError = Error) is a catastrophic hardware/JVM failure that user code isn't expected to fix mid-flight.
java.lang.Throwable: Error (severe JVM failure, unchecked) and Exception, which splits into Checked Exceptions (enforced at compile time) and **RuntimeException (Unchecked)**.3. Essential Exam Definitions
java.lang.Throwable.Exception subclass (EXCLUDING RuntimeException and Error) checked strictly by javac at compile time; the programmer MUST handle it with try-catch or declare it via throws (IOException, SQLException, ClassNotFoundException).RuntimeException (or Error) that is NOT checked by javac at compile time, typically caused by programmer logic mistakes (ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException).4. Detailed Technical Explanation & Syntax
1. Compile-Time Errors vs Runtime Errors vs Exceptions
Compile-time errors (syntax errors like missing semicolons or type mismatches) are caught by javac before .class is even produced. Runtime errors occur while running .class on the JVM—Java turns recoverable runtime conditions into Exception objects so your code can catch and recover cleanly.
2. The Throwable Class Hierarchy
All exception types subclass java.lang.Throwable (which itself extends Object). Throwable splits into two main branches:
- Branch 1 —
Error: Serious JVM environment failures (StackOverflowError,OutOfMemoryError,VirtualMachineError). Programs normally do NOT catch these. - Branch 2 —
Exception: Exceptional conditions user programs should catch or declare. Splits into: - -> Checked Exceptions (
Exceptionsubclasses excludingRuntimeException):IOException,FileNotFoundException,SQLException,ClassNotFoundException,InterruptedException. - -> Unchecked Exceptions (
RuntimeExceptionsubclasses):ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException,NumberFormatException,IllegalArgumentException.
3. Five Exception-Handling Keywords & Uncaught Exceptions
Java manages exception handling using five reserved keywords: **try, catch, throw, throws, and finally**.
If a method divides by zero (10 / 0) without a try-catch, the JVM creates an ArithmeticException object, stops executing further statements in main(), hands it to the Default Handler, prints Exception in thread "main" java.lang.ArithmeticException: / by zero + stack trace, and terminates the program.
5. Working Principle: Compiler Rule: Checked vs Unchecked Verification
- When
javaccompiles a call likeFileReader f = new FileReader("data.txt");, it seesFileNotFoundException(Checked). - If neither
try-catchnorthrows FileNotFoundExceptionwraps it, compilation FAILS immediately (unreported exception). - When
javaccompilesint x = 10 / 0;,ArithmeticExceptionis aRuntimeException(Unchecked), so compilation SUCCEEDS—failure occurs at runtime if unhandled.
6. Examination-Ready Diagrams
Java Exception Class Hierarchy
Complete java.lang.Throwable class tree partitioning runtime issues into Error (unchecked JVM failures) and Exception (Checked vs RuntimeException Unchecked).
- Object -> Throwable: Root superclass of all errors and exceptions; only
Throwablesubclasses can be thrown/caught. - Error (Unchecked): Severe JVM/environment failures like
StackOverflowError,OutOfMemoryError(not caught normally). - Exception (Checked): Compile-time verified exceptions like
IOException,SQLException,ClassNotFoundException. - RuntimeException (Unchecked): Logic/programming bugs checked only at runtime:
ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException.
java.lang.Objectis parent ofjava.lang.Throwable.Throwablesplits into two main branches:ErrorandException.- Under
Exception,RuntimeException(and its subclasses) are Unchecked; all otherExceptionsubclasses are Checked.
- Always start from
Object->Throwableat the top. - Split cleanly into two branches:
Exception(left) andError(right). - Under
Exception, clearly draw two sub-groups:Checked Exceptions(IOException,SQLException,FileNotFoundException,ClassNotFoundException) andRuntimeException (Unchecked)(ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException).
7. Complete Java Code Examples & Tracing
ArithmeticException (unchecked) and required handling for ClassNotFoundException (checked).| 1 | public class CheckedVsUncheckedDemo { |
| 2 | public static void main(String[] args) { |
| 3 | // 1. Unchecked Exception (RuntimeException): compiler does not force try-catch |
| 4 | try { |
| 5 | int a = 10, b = 0; |
| 6 | int c = a / b; // Throws ArithmeticException here! |
| 7 | System.out.println("This line never executes: " + c); |
| 8 | } catch (ArithmeticException e) { |
| 9 | System.out.println("Caught Unchecked Exception: " + e.toString()); |
| 10 | } |
| 11 | |
| 12 | // 2. Checked Exception: MUST be caught or declared with 'throws' |
| 13 | try { |
| 14 | Class.forName("com.missing.FakeDriver"); |
| 15 | } catch (ClassNotFoundException e) { |
| 16 | System.out.println("Caught Checked Exception: " + e.getMessage()); |
| 17 | } |
| 18 | |
| 19 | System.out.println("Program recovered and finished gracefully!"); |
| 20 | } |
| 21 | } |
Caught Unchecked Exception: java.lang.ArithmeticException: / by zero Caught Checked Exception: com.missing.FakeDriver Program recovered and finished gracefully!
ThrowableExceptionRuntimeExceptionErrortrycatch- Thinking
RuntimeExceptionis a Checked exception (it is the root of UNCHECKED exceptions!). - Thinking
Errorcan/should be caught routinely withcatch (Throwable t).
Comparison Tables
| Criterion | Checked Exceptions | Unchecked Exceptions |
|---|---|---|
| Checking Time | Checked at COMPILE TIME by javac | Checked at RUNTIME by JVM |
| Base Class | Exception (excluding RuntimeException) | RuntimeException (and Error) |
| Handling Requirement | Mandatory (try-catch or throws) | Optional (not enforced by compiler) |
| Typical Cause | External failures (missing file, broken DB/network) | Programming bugs (null dereference, /0, bad index) |
| Common Examples | IOException, FileNotFoundException, SQLException, ClassNotFoundException, InterruptedException | ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException |
Real-World Applications
- File parsers catch
IOException/FileNotFoundExceptionto prompt users politely instead of crashing. - Network/JDBC calls handle
SQLExceptionto roll back database transactions safely.
Important KTU Exam Points
- Always draw Diagram 9 starting at
Object->Throwable->Exception&Error->CheckedvsRuntimeException (Unchecked)! - List at least 4 Checked and 4 Unchecked exception class names accurately.
Memory Techniques & Revision
- 5 Exception Keywords — 'T-C-T-T-F':
try,catch,throw,throws,finally. - Checked = 'External I/O/DB/Class' (
IO,SQL,ClassNotFound,Interrupted); Unchecked = 'Logic Bugs' (Arithmetic,NullPointer,ArrayIndex).
Common Pitfalls to Avoid
- Forgetting that
Error(e.g.StackOverflowError) also falls under unchecked throwables.
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
Introduction: An exception is an abnormal runtime condition represented by an object descending from java.lang.Throwable. Java categorizes runtime problems into Checked Exceptions, Unchecked Exceptions, and Errors.
- Hierarchy:
Object -> Throwable, splitting intoError(JVM/environment failures likeStackOverflowError) andException. - Checked Exceptions: Subclasses of
ExceptionoutsideRuntimeException(IOException,SQLException,FileNotFoundException,ClassNotFoundException). Verified at compile time; must be handled viatry-catchor declared withthrows. - Unchecked Exceptions: Subclasses of
RuntimeException(ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException) plusError. Not checked at compile time; usually arise from logic errors.
Quick Understanding Check
1. Which of the following is a CHECKED exception in Java?
2. What is the ultimate common superclass of BOTH Error and Exception?