DEV Community

PRIYA K
PRIYA K

Posted on

Types of Exception in Java with Examples

1. ArithmeticException
Occurs when arithmetic operation is invalid.
Example: divide by zero.

public class Main {
    public static void main(String[] args) {

        try {
            int a = 10 / 0;
        }

        catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2. ArrayIndexOutOfBoundsException
Occurs when array index is invalid.

public class Main {
    public static void main(String[] args) {

        int[] arr = {1, 2, 3};

        try {
            System.out.println(arr[5]);
        }

        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Invalid array index");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3. ClassNotFoundException
Occurs when class definition is not found.

public class Main {
    public static void main(String[] args) {

        try {
            Class.forName("Demo");
        }

        catch (ClassNotFoundException e) {
            System.out.println("Class not found");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

4. FileNotFoundException
Occurs when file does not exist.

import java.io.*;
public class Main {
    public static void main(String[] args) {

        try {
            FileReader f = new FileReader("abc.txt");
        }

        catch (FileNotFoundException e) {
            System.out.println("File not found");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

5. IOException
Occurs during input/output operation failure.

import java.io.*;
public class Main {
    public static void main(String[] args) {

        try {
            FileReader f = new FileReader("abc.txt");
            f.read();
        }

        catch (IOException e) {
            System.out.println("Input Output Error");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

6. InterruptedException
Occurs when a thread is interrupted.

public class Main {
    public static void main(String[] args) {

        try {
            Thread.sleep(2000);
        }

        catch (InterruptedException e) {
            System.out.println("Thread interrupted");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

7. NoSuchFieldException
Occurs when field is not found.

class Student {
    int age;
}

public class Main {
    public static void main(String[] args) {

        try {
            Student.class.getField("name");
        }

        catch (NoSuchFieldException e) {
            System.out.println("Field not found");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

8. NoSuchMethodException
Occurs when method is not found.

class Demo {
    void show() {}
}

public class Main {
    public static void main(String[] args) {

        try {
            Demo.class.getMethod("display");
        }

        catch (NoSuchMethodException e) {
            System.out.println("Method not found");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

9. NullPointerException
Occurs when accessing null object.

public class Main {
    public static void main(String[] args) {

        String s = null;

        try {
            System.out.println(s.length());
        }

        catch (NullPointerException e) {
            System.out.println("Object is null");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

10. NumberFormatException
Occurs when string cannot convert to number.

public class Main {
    public static void main(String[] args) {

        try {
            int num = Integer.parseInt("abc");
        }

        catch (NumberFormatException e) {
            System.out.println("Invalid number format");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

11. RuntimeException
General runtime exception.

public class Main {
    public static void main(String[] args) {

        try {
            throw new RuntimeException("Runtime Error");
        }

        catch (RuntimeException e) {
            System.out.println(e.getMessage());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

12. StringIndexOutOfBoundsException
Occurs when string index is invalid.

public class Main {
    public static void main(String[] args) {

        String s = "Java";

        try {
            System.out.println(s.charAt(10));
        }

        catch (StringIndexOutOfBoundsException e) {
            System.out.println("Invalid string index");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

13. IllegalArgumentException
Occurs when invalid argument is passed.

public class Main {
    static void checkAge(int age) {
        if (age < 18) {
            throw new IllegalArgumentException("Age must be 18 or above");
        }
        System.out.println("Eligible");
    }
    public static void main(String[] args) {
        try {
            checkAge(15);
        }
        catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

14. IllegalStateException
Occurs when method is called at wrong time/state.

public class Main {
    static boolean isStarted = false;
    static void start() {
        if (isStarted) {
            throw new IllegalStateException("Already started");
        }
        isStarted = true;
        System.out.println("Started");
    }
    public static void main(String[] args) {
        try {
            start();
            start();
        }
        catch (IllegalStateException e) {
            System.out.println(e.getMessage());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Arithmetic: Math error
ArrayIndexOutOfBounds: Wrong array index
StringIndexOutOfBounds: Wrong string index
NullPointer: Null object used
NumberFormat: Invalid number conversion
FileNotFound: File missing
IOException: Input/output issue
IllegalArgument: Wrong input given
IllegalState: Wrong object state

Top comments (0)