Basics Of Exceptions

1. What Is an Exception?

int result = 10 / 0;

This causes Java to throw an exception because division by zero is not valid for integers.


2. How Java Handles an Exception

When an exception is thrown:

Exception occurs
      ↓
Exception object is created
      ↓
Java searches for a handler
      ↓
Current method
      ↓
Calling method
      ↓
Further up the call stack
      ↓
Handler found?
   ↙       ↘
 Yes        No
 ↓           ↓
catch       Thread terminates

3. Catch or Specify Requirement

For checked exceptions, Java requires you to either:

Catch the exception

try {
    // code that may throw an exception
} catch (Exception e) {
    // handle exception
}

Or specify the exception

void readFile() throws IOException {
    // code that may throw IOException
}

So:

Catch it or declare it with throws.

If you do neither for a checked exception, the code will not compile.


4. Three Types of Exceptions

Java's exception hierarchy can be simplified as:

Throwable
├── Error
└── Exception
    ├── RuntimeException
    └── Other checked exceptions

A. Checked Exceptions

IOException
FileNotFoundException
SQLException

Example:

void readFile() throws IOException {
    // ...
}

B. Runtime Exceptions

Examples:

NullPointerException
ArrayIndexOutOfBoundsException
IllegalArgumentException
ArithmeticException

Example:

String name = null;
System.out.println(name.length()); // NullPointerException

C. Errors

Examples:

OutOfMemoryError
StackOverflowError

5. Checked vs. Unchecked Exceptions

Type Must catch/declare? Typical meaning
Checked Exception Yes Expected/recoverable condition
RuntimeException No Programming/API error
Error No Serious system/JVM problem

RuntimeException and Error are collectively called unchecked exceptions.

Throwable
│
├── Error ──────────────── Unchecked
│
└── Exception
    │
    ├── RuntimeException ─ Unchecked
    │
    └── Other Exceptions ─ Checked

6. Why Does Java Have Checked Exceptions?

Checked exceptions force developers to explicitly think about certain failures.

For example:

void readFile() throws IOException

tells whoever calls the method:

"This method may fail because of an I/O problem. You need to deal with it."

This helps make error handling explicit and improves program reliability.


7. Important Terms

Term Meaning
Exception Event that disrupts normal execution
Throw Create/send an exception
Catch Handle an exception
Exception handler Code that handles an exception
Call stack Chain of methods that led to the current execution
Checked exception Must be caught or declared
Unchecked exception Does not have to be caught or declared
throws Declares that a method may throw an exception
try Contains code that may throw an exception
catch Handles a thrown exception

8. Key Idea to Remember

The most important distinction is:

Checked Exception
        ↓
Must catch OR declare

RuntimeException / Error
        ↓
No catch-or-specify requirement

So, in simple terms:

Exceptions are Java's mechanism for reporting and handling problems during program execution. Checked exceptions must be handled or declared, while runtime exceptions and errors are unchecked.