← Back to Home

Exception

An exception in Java is an unexpected event that occurs during program execution and disrupts the normal flow of the program. Java provides a robust exception handling mechanism to detect, handle, and recover from such runtime problems.

This is a high-frequency interview topic and critical for writing reliable, production-ready Java applications.

What Is an Exception?

  • An abnormal condition during execution
  • Occurs at runtime
  • Causes program termination if not handled
  • Represented as an object in Java
int a = 10 / 0; // ArithmeticException
          

Why Exceptions Occur

Common causes include:

  • Invalid input
  • Divide by zero
  • Accessing invalid array index
  • Null object access
  • File or network issues

Exception vs Error (Important Distinction)

Aspect Exception Error
Nature Recoverable Non-recoverable
Occurs due to Program logic / runtime issues JVM or system failure
Handling Can be handled Should not be handled
Example NullPointerException OutOfMemoryError

Exception Hierarchy (Interview Favorite)

Throwable
 ├── Exception
 │    ├── Checked Exceptions
 │    │    └── IOException
 │    └── Unchecked Exceptions
 │         └── RuntimeException
 │              ├── NullPointerException
 │              ├── ArithmeticException
 │              └── ArrayIndexOutOfBoundsException
 └── Error
          

Types of Exceptions in Java

1️⃣ Checked Exceptions (Compile-Time)

  • Checked at compile time
  • Must be handled or declared
  • Usually external or recoverable issues
FileReader fr = new FileReader("file.txt"); // IOException
          

Examples:

  • IOException
  • SQLException
  • ClassNotFoundException

2️⃣ Unchecked Exceptions (Runtime)

  • Occur at runtime
  • Not checked by compiler
  • Usually programming mistakes
int[] arr = new int[3];
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException
          

Examples:

  • NullPointerException
  • ArithmeticException
  • NumberFormatException

3️⃣ Errors

  • Serious JVM/system problems
  • Not meant to be handled

Examples:

  • OutOfMemoryError
  • StackOverflowError

What Happens When an Exception Occurs

  1. Exception object is created
  2. JVM looks for handling code
  3. If found → exception handled
  4. If not found → program terminates abnormally

Why Exception Handling Is Important

  • Prevents program crash
  • Improves reliability
  • Enables graceful recovery
  • Helps debugging
  • Mandatory for real-world applications

Exception Handling Keywords (Preview)

Java provides five keywords:

  • try
  • catch
  • finally
  • throw
  • throws

(Handled in next topics)

Exception vs Bug vs Error

Term Meaning
Bug Coding mistake
Exception Runtime abnormal event
Error JVM/system failure

Common Beginner Misconceptions

  • Thinking all exceptions are errors
  • Ignoring unchecked exceptions
  • Catching generic Exception everywhere
  • Assuming exceptions occur only in bad code

Interview-Ready Answers

Short Answer

An exception is an abnormal event that occurs during program execution and disrupts normal flow.

Detailed Answer

In Java, an exception is an object that represents an error condition occurring at runtime. Java provides an exception handling mechanism using try-catch blocks to handle such events gracefully and prevent program termination.

Key Takeaway

Exceptions are runtime problems, not system failures. Proper exception handling ensures robust, stable, and maintainable Java applications.