DevNotes
Support
Exception Handling · Chapter 01

Exception Handling Fundamentals

What exceptions are, Error vs Exception, the Throwable hierarchy, stack traces, propagation and the five keywords.

◷ 6 min read§ 15 sections? 8 interview questions{ } 9 code examples

#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.

Java
int a = 10;
int b = 0;
System.out.println(a / b);
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero
Without exception handling
Application Starts
Exception Occurs
Application Terminates
With exception handling
Application Starts
Exception Occurs
Handle Exception
Continue Execution

#Why Do We Need Exception Handling?

Imagine an online banking application:

Java
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.

ErrorException
Serious problemRecoverable problem
JVM cannot usually recoverApplication can recover
Programmer usually doesn't handleShould be handled
Extends ErrorExtends Exception
Example ErrorsExample Exceptions
StackOverflowErrorIOException
OutOfMemoryErrorSQLException
VirtualMachineErrorNullPointerException
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

MethodPurpose
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
Java
try {
    int x = 10 / 0;
} catch (Exception e) {
    System.out.println(e.getMessage());
    System.out.println(e);
    e.printStackTrace();
}
Output
/ by zero
java.lang.ArithmeticException: / by zero
(stack trace)

#Exception Flow

Java
public class Demo {
    public static void main(String[] args) {
        System.out.println("Start");
        int a = 10 / 0;
        System.out.println("End");
    }
}
Start printed
10 / 0
JVM creates an ArithmeticException object
Searches for a matching catch block
Found?
program terminates
handled
Output
Start
Exception in thread "main" java.lang.ArithmeticException: / by zero

Notice that End never executes.

#Anatomy of an Exception Object

Every exception contains a type, a message, a stack trace and an optional cause.

Java
throw new IllegalArgumentException("Invalid ID");
PartValue
TypeIllegalArgumentException
MessageInvalid ID
Causenull
Stack traceThe method calls that led here

#Stack Trace Explained

Java
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();
    }
}
Output
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

ExceptionExample
NullPointerExceptionString s = null; s.length();
ArithmeticExceptionint x = 10 / 0;
ArrayIndexOutOfBoundsExceptionint[] arr = {1, 2}; arr[5];
NumberFormatExceptionInteger.parseInt("ABC");
ClassCastExceptionObject obj = "Java"; Integer i = (Integer) obj;
IllegalArgumentExceptionThread.sleep(-10);

IllegalStateException

Java
Iterator<Integer> iterator = new ArrayList<>(List.of(1, 2)).iterator();
iterator.remove();   // remove() before next()
Output
IllegalStateException

#Exception Handling Keywords

Java provides five important keywords.

KeywordPurposeExample
tryContains risky codetry { … }
catchHandles exceptionscatch (Exception e) { … }
finallyAlways executes (except in cases like System.exit())finally { … }
throwExplicitly throws an exceptionthrow new RuntimeException();
throwsDeclares that a method may throw an exceptionpublic void readFile() throws IOException

Basic Example

Java
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");
    }
}
Output
Cannot divide by zero
Program Continues

#Exception Propagation

Java
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");
        }
    }
}
main() → catchHandled
method1()
method2()
method3()exception thrown here
The exception propagates up the call stack until a matching catch block is found

#Best Practices

  • Catch the most specific exception first — catch (IOException e) instead of catch (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 Throwable class
  • Exception object anatomy
  • Stack traces
  • Exception propagation
  • try, catch, finally, throw and throws
  • Common runtime exceptions
  • Exception handling best practices