Exception Handling in Java Explained in Depth with Examples (Beginner to Advanced)

Exception Handling in Java Explained in Depth

Author: Gursehbaj Singh | Blog: DevMode

Exception Handling is one of the most important topics in Java. It helps a program handle runtime errors so that the program does not crash and can continue running safely.

What is an Exception?

An exception is an unwanted event that occurs during program execution and disrupts the normal flow of the program.

Examples of exceptions:

  • Dividing a number by zero
  • Accessing an invalid array index
  • Opening a file that does not exist

Why Exception Handling is Important?

  • Prevents program from crashing
  • Helps find errors easily
  • Improves program reliability
  • Makes code professional and safe

Types of Exceptions in Java

  • Checked Exceptions (Compile-time)
  • Unchecked Exceptions (Runtime)
  • Errors (System-level problems)

1. try and catch Block

The try block contains risky code. The catch block handles the error.


try {
    int a = 10;
    int b = 0;
    int result = a / b;
    System.out.println(result);
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");
}

2. Multiple catch Blocks


try {
    int[] arr = {1, 2, 3};
    System.out.println(arr[5]);
} catch (ArithmeticException e) {
    System.out.println("Math error");
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Array index is invalid");
} catch (Exception e) {
    System.out.println("General exception occurred");
}

3. finally Block

The finally block always runs whether exception occurs or not.


try {
    int num = 10 / 2;
    System.out.println(num);
} catch (Exception e) {
    System.out.println("Error occurred");
} finally {
    System.out.println("This block always executes");
}

4. Nested try-catch


try {
    try {
        int a = 5 / 0;
    } catch (ArithmeticException e) {
        System.out.println("Inner catch: Division by zero");
    }
} catch (Exception e) {
    System.out.println("Outer catch");
}

Common Runtime Exceptions

  • ArithmeticException
  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • NumberFormatException

NullPointerException Example


String name = null;

try {
    System.out.println(name.length());
} catch (NullPointerException e) {
    System.out.println("String is null");
}

NumberFormatException Example


try {
    int num = Integer.parseInt("abc");
} catch (NumberFormatException e) {
    System.out.println("Invalid number format");
}

Flow of Exception Handling

  1. Program enters try block
  2. If error occurs, control goes to catch block
  3. finally block executes
  4. Program continues normally

Best Practices (Part 1)

  • Use specific exception classes
  • Do not write empty catch blocks
  • Always close resources in finally

Part 2 will include:

  • throw keyword
  • throws keyword
  • Custom Exceptions
  • User-defined Exception Example
  • Real-life use cases
  • Advanced best practices
  • Interview questions
  • Conclusion

Comments

Popular posts from this blog

Top 10 Free Coding Websites Every Beginner Should Use in 2026

Graph Data Structure – Complete Beginner to Advanced Guide with BFS, DFS and Examples

5 JavaScript Console Methods You're Not Using (But Should Be)