Exception Handling Fundamentals
What exceptions are, Error vs Exception, the Throwable hierarchy, stack traces, propagation and the five keywords.
#What is Exception Handling?
An exception is an event that interrupts the normal execution of a program. Instead of crashing the application, Java provides a mechanism to detect, handle and recover from runtime problems.
int a = 10;
int b = 0;
System.out.println(a / b);Exception in thread "main" java.lang.ArithmeticException: / by zero#Why Do We Need Exception Handling?
Imagine an online banking application:
withdrawMoney();
updateBalance();
sendSMS();Suppose withdrawMoney() throws an exception. Without exception handling:
- The balance may not update.
- The SMS won't be sent.
- The transaction becomes inconsistent.
Proper exception handling allows us to:
- Recover gracefully
- Roll back transactions
- Log errors
- Notify users
- Keep the application running
#Error vs Exception
One of the most common interview questions.
| Error | Exception |
|---|---|
| Serious problem | Recoverable problem |
| JVM cannot usually recover | Application can recover |
| Programmer usually doesn't handle | Should be handled |
Extends Error | Extends Exception |
| Example Errors | Example Exceptions |
|---|---|
StackOverflowError | IOException |
OutOfMemoryError | SQLException |
VirtualMachineError | NullPointerException |
ArithmeticException |
#Java Exception Hierarchy
- Object
- Throwable
- Error
- Exception
- RuntimeException
Expanded Hierarchy
- Throwable
- Error
- OutOfMemoryError
- StackOverflowError
- VirtualMachineError
- Exception
- IOException
- SQLException
- ClassNotFoundException
- RuntimeException
- NullPointerException
- ArithmeticException
- ArrayIndexOutOfBoundsException
- IllegalArgumentException
- NumberFormatException
- IllegalStateException
#Throwable Class
Every exception and error inherits from Throwable.
Important Methods
| Method | Purpose |
|---|---|
getMessage() | The detail message |
printStackTrace() | Prints the type, message and stack trace |
getCause() | The wrapped (root) cause, if any |
fillInStackTrace() | Records the current stack trace |
toString() | Class name + message |
try {
int x = 10 / 0;
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e);
e.printStackTrace();
}/ by zero
java.lang.ArithmeticException: / by zero
(stack trace)#Exception Flow
public class Demo {
public static void main(String[] args) {
System.out.println("Start");
int a = 10 / 0;
System.out.println("End");
}
}Start
Exception in thread "main" java.lang.ArithmeticException: / by zeroNotice that End never executes.
#Anatomy of an Exception Object
Every exception contains a type, a message, a stack trace and an optional cause.
throw new IllegalArgumentException("Invalid ID");| Part | Value |
|---|---|
| Type | IllegalArgumentException |
| Message | Invalid ID |
| Cause | null |
| Stack trace | The method calls that led here |
#Stack Trace Explained
public class Demo {
static void method3() {
int x = 10 / 0;
}
static void method2() {
method3();
}
static void method1() {
method2();
}
public static void main(String[] args) {
method1();
}
}java.lang.ArithmeticException: / by zero
at Demo.method3(Demo.java:3)
at Demo.method2(Demo.java:6)
at Demo.method1(Demo.java:9)
at Demo.main(Demo.java:12)Read from top to bottom. The first at line shows exactly where the exception occurred.
#Types of Exceptions
Java divides exceptions into two categories:
- Checked exceptions
- Unchecked exceptions
We'll study these in detail in the next chapter.
#Common Runtime Exceptions
| Exception | Example |
|---|---|
NullPointerException | String s = null; s.length(); |
ArithmeticException | int x = 10 / 0; |
ArrayIndexOutOfBoundsException | int[] arr = {1, 2}; arr[5]; |
NumberFormatException | Integer.parseInt("ABC"); |
ClassCastException | Object obj = "Java"; Integer i = (Integer) obj; |
IllegalArgumentException | Thread.sleep(-10); |
IllegalStateException
Iterator<Integer> iterator = new ArrayList<>(List.of(1, 2)).iterator();
iterator.remove(); // remove() before next()IllegalStateException#Exception Handling Keywords
Java provides five important keywords.
| Keyword | Purpose | Example |
|---|---|---|
try | Contains risky code | try { … } |
catch | Handles exceptions | catch (Exception e) { … } |
finally | Always executes (except in cases like System.exit()) | finally { … } |
throw | Explicitly throws an exception | throw new RuntimeException(); |
throws | Declares that a method may throw an exception | public void readFile() throws IOException |
Basic Example
public class Demo {
public static void main(String[] args) {
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
System.out.println("Program Continues");
}
}Cannot divide by zero
Program Continues#Exception Propagation
class Demo {
static void method3() {
int x = 10 / 0;
}
static void method2() {
method3();
}
static void method1() {
method2();
}
public static void main(String[] args) {
try {
method1();
} catch (Exception e) {
System.out.println("Handled");
}
}
}#Best Practices
- Catch the most specific exception first —
catch (IOException e)instead ofcatch (Exception e)whenever possible. - Log the exception with its stack trace:
logger.error("Failed to process request", e); - Don't swallow exceptions. An empty
catch (Exception e) { }hides failures; at minimum log it:catch (Exception e) { logger.error("Error", e); } - Use exceptions for exceptional situations, not normal control flow. Avoid relying on exceptions when simple validation can prevent the error.
#Frequently Asked Interview Questions
Q1What is an exception?
An event that disrupts the normal flow of program execution. It is represented by an object derived from Throwable.
Q2What is the difference between Error and Exception?
Errors are serious JVM/system-level problems that applications generally should not handle. Exceptions represent conditions that applications can often handle or recover from.
Q3What is the root class of all exceptions?
Throwable.
Q4What happens when an exception occurs?
The JVM creates an exception object, searches the call stack for a matching catch block, and if none is found, terminates the thread and prints the stack trace.
Q5What information does an exception object contain?
- Exception type
- Message
- Stack trace
- Cause (optional)
Q6What is exception propagation?
The process by which an exception travels up the method call stack until it is handled or reaches the JVM.
Q7Which keywords are used in exception handling?
try, catch, finally, throw and throws.
Q8Why shouldn't we catch Exception everywhere?
Because it hides the actual problem, can unintentionally swallow programming bugs, and makes debugging and maintenance harder. Catch the most specific exception you can handle.
#Chapter Summary
- Why exception handling is necessary
- Error vs Exception
- Java exception hierarchy
- The
Throwableclass - Exception object anatomy
- Stack traces
- Exception propagation
try,catch,finally,throwandthrows- Common runtime exceptions
- Exception handling best practices