DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java OOPTopic 54 of 59
←PreviousPrevNextNext→

Java Exception Handling

Introduction to Exception Handling

Exception handling in Java is a mechanism to handle runtime errors, maintaining the normal flow of the application. An exception is an event that disrupts the normal flow of the program.

Java exceptions are categorized into two types:

1. Checked exceptions - compile-time exceptions that must be handled

2. Unchecked exceptions - runtime exceptions that don't need to be explicitly handled

Exception Hierarchy

Exception TypeDescriptionExamples
Checked ExceptionsExceptions checked at compile timeIOException, SQLException, ClassNotFoundException
Unchecked ExceptionsExceptions not checked at compile timeNullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException
ErrorsSerious problems that applications should not try to catchOutOfMemoryError, StackOverflowError, VirtualMachineError

try-catch-finally Blocks

The try block contains code that might throw an exception. The catch block handles the exception. The finally block always executes, whether an exception occurred or not.

Example
public class ExceptionHandlingExample {
    public static void main(String[] args) {
        try {
            int[] numbers = {1, 2, 3};
            System.out.println("Accessing element at index 5: " + numbers[5]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception caught: " + e.getMessage());
        } finally {
            System.out.println("This block always executes");
        }
        
        System.out.println("Program continues after exception handling");
    }
}
Output
Exception caught: Index 5 out of bounds for length 3
This block always executes
Program continues after exception handling

Multiple catch Blocks

You can have multiple catch blocks to handle different types of exceptions. The catch blocks are evaluated in order, so more specific exceptions should come before more general ones.

Example
public class MultipleCatchExample {
    public static void main(String[] args) {
        try {
            int a = 10;
            int b = 0;
            int result = a / b;
            
            String str = null;
            System.out.println(str.length());
        } catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught: " + e.getMessage());
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("General Exception caught: " + e.getMessage());
        }
    }
}
Output
ArithmeticException caught: / by zero

throw and throws Keywords

The throw keyword is used to explicitly throw an exception. The throws keyword is used in method signatures to declare that a method might throw exceptions.

Example
class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class ThrowThrowsExample {
    public static void validateAge(int age) throws CustomException {
        if (age < 18) {
            throw new CustomException("Age must be 18 or older");
        } else {
            System.out.println("Age is valid");
        }
    }
    
    public static void main(String[] args) {
        try {
            validateAge(15);
        } catch (CustomException e) {
            System.out.println("Caught custom exception: " + e.getMessage());
        }
        
        try {
            validateAge(20);
        } catch (CustomException e) {
            System.out.println("Caught custom exception: " + e.getMessage());
        }
    }
}
Output
Caught custom exception: Age must be 18 or older
Age is valid

try-with-resources

The try-with-resources statement automatically closes resources that implement the AutoCloseable interface. This ensures proper resource management and avoids resource leaks.

Example
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TryWithResourcesExample {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println("IOException caught: " + e.getMessage());
        }
    }
}

Best Practices

  • ✅ Catch specific exceptions rather than using generic Exception catching
  • ✅ Use finally blocks to clean up resources (or use try-with-resources)
  • ✅ Don't swallow exceptions - at least log them
  • ✅ Create custom exceptions for business logic errors
  • ✅ Use exception chaining to preserve the original exception
  • ✅ Avoid using exceptions for control flow - they're expensive operations
Test your knowledge: Java Exception Handling
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java OOPTopic 54 of 59
←PreviousPrevNextNext→