NBKRIST – Java Hub

Java Exception Handling (Detailed Guide)

Expert View

As a software engineer, I realized that exceptions aren’t simply β€œerrors” β€” they are a structural way to make software more resilient. In real-world enterprise applications, exceptions are logged, reported, and often recovered gracefully. Understanding this mechanism helps you build professional-grade software systems that don’t crash, but respond intelligently to unexpected scenarios.

1. Introduction

Exception handling allows Java programs to detect and respond to runtime anomalies without crashing. It maintains program flow and improves code readability by separating normal logic from error-handling code.

2. Hierarchy of Standard Exception Classes

All exceptions are subclasses of the Throwable class. The Error and Exception branches categorize issues as either system-level or program-level respectively.

Throwable β”‚ β”œβ”€β”€ Error β”‚ β”œβ”€β”€ OutOfMemoryError β”‚ β”œβ”€β”€ StackOverflowError β”‚ └── VirtualMachineError β”‚ └── Exception β”œβ”€β”€ IOException β”‚ β”œβ”€β”€ FileNotFoundException β”‚ └── EOFException β”œβ”€β”€ SQLException β”œβ”€β”€ ClassNotFoundException β”œβ”€β”€ InterruptedException β”œβ”€β”€ RuntimeException β”‚ β”œβ”€β”€ NullPointerException β”‚ β”œβ”€β”€ ArithmeticException β”‚ β”œβ”€β”€ ArrayIndexOutOfBoundsException β”‚ └── NumberFormatException

3. Checked vs Unchecked Exceptions

Checked Exceptions

Checked exceptions are verified by the compiler at compile time. They indicate conditions that can be anticipated and handled in advance (e.g., missing files or database failures).

import java.io.*; class CheckedExample { public static void main(String[] args) { try { FileReader fr = new FileReader("test.txt"); fr.read(); } catch (IOException e) { System.out.println("Handled checked exception: " + e.getMessage()); } } }

Unchecked Exceptions

Unchecked exceptions are not verified by the compiler. They usually arise due to logic errors or unexpected inputs during runtime.

class UncheckedExample { public static void main(String[] args) { int[] arr = {1, 2, 3}; System.out.println(arr[5]); } }

Why distinction matters: Checked exceptions represent predictable external conditions; unchecked exceptions reflect programmer mistakes.

4. try, catch, and finally Blocks

These keywords work together to handle exceptions and ensure critical code executes, regardless of errors.

public class TryCatchFinallyDemo { public static void main(String[] args) { try { int result = 5 / 0; } catch (ArithmeticException e) { System.out.println("Exception caught: " + e.getMessage()); } finally { System.out.println("Finally block executed."); } } }

5. throw and throws Keywords

throw is used to explicitly raise exceptions, while throws declares potential exceptions for caller awareness.

class Voting { static void checkAge(int age) throws ArithmeticException { if (age < 18) throw new ArithmeticException("Not eligible to vote"); else System.out.println("Eligible to vote!"); } public static void main(String[] args) { try { checkAge(15); } catch (ArithmeticException e) { System.out.println("Handled: " + e.getMessage()); } } }

Practice & Interview Questions

1. What is the root class for all exceptions?
Throwable
2. Why are I/O errors checked exceptions?
Because they depend on external resources (files, devices) beyond program control.
3. Why are logic errors unchecked?
They arise from programming mistakes, not external failures.
4. What happens if finally block throws an exception?
It can override exceptions from the try or catch block if not handled.

πŸ”‘ Key Summary

Mastering exception handling is not just about syntax β€” it’s about writing stable, predictable, and professional-grade Java applications.