DEV Community

Ranjith Ranjith
Ranjith Ranjith

Posted on

RUN TIME EXCEPTION

1) ArithmeticException: It is thrown when an exceptional condition has occurred in an arithmetic operation.
ex public class Example1 {
public static void main(String[] args) {
int a = 10, b = 0;
System.out.println(a / b); // divide by zero
}
}

2) ArrayIndexOutOfBoundsException: It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
ex
public class Example2 {
public static void main(String[] args) {
int arr[] = {1, 2, 3};
System.out.println(arr[5]);
}
}

3) ClassNotFoundException: This Exception is raised when we try to access a class whose definition is not found
ex
public class Example3 {
public static void main(String[] args) {
try {
Class.forName("MyClass");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

4) FileNotFoundException: This Exception is raised when a file is not accessible or does not open.
ex
import java.io.File;
import java.io.FileReader;

public class Example4 {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader(new File("abc.txt"));
}
}

5) IOException: It is thrown when an input-output operation failed or interrupted
ex
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Example5 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.close();
br.readLine(); // Already closed => IOException
}
}

6) InterruptedException: It is thrown when a thread is waiting, sleeping, or doing some processing, and it is interrupted.
ex
public class Example6 extends Thread {
public void run() {
try {
Thread.sleep(2000); // wait
System.out.println("Thread finished");
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
}

public static void main(String[] args) {
    Example6 t = new Example6();
    t.start();
    t.interrupt(); 
}
Enter fullscreen mode Exit fullscreen mode

}

7) NoSuchFieldException: It is thrown when a class does not contain the field (or variable) specified
ex
import java.lang.reflect.Field;

class Student {
int id;
}

public class Example7 {
public static void main(String[] args) throws Exception {
Field f = Student.class.getDeclaredField("name");
}
}

8) NoSuchMethodException: It is thrown when accessing a method that is not found.
ex
import java.lang.reflect.Method;

class Student {
public void display() {}
}

public class Example8 {
public static void main(String[] args) throws Exception {
Method m = Student.class.getDeclaredMethod("show");
}
}

9) NullPointerException: This exception is raised when referring to the members of a null object. Null represents nothing

public class Example9 {
public static void main(String[] args) {
String s = null;
System.out.println(s.length()); // null object access
}
}

10) NumberFormatException: This exception is raised when a method could not convert a string into a numeric format.
ex
public class Example10 {
public static void main(String[] args) {
String s = "abc";
int num = Integer.parseInt(s); //
}
}

11) RuntimeException: This represents an exception that occurs during runtime.
ex
public class Example11 {
public static void main(String[] args) {
throw new RuntimeException("Runtime exception example");
}
}

12) StringIndexOutOfBoundsException: It is thrown by String class methods to indicate that an index is either negative or greater than the size of the string
ex
public class Example12 {
public static void main(String[] args) {
String s = "java";
System.out.println(s.charAt(10));
}
}

13) IllegalArgumentException : This exception will throw the error or error statement when the method receives an argument which is not accurately fit to the given relation or condition. It comes under the unchecked exception.
ex
public class Example13 {
public static void main(String[] args) {
Thread t = new Thread();
t.setPriority(20);
}
}

14) IllegalStateException : This exception will throw an error or error message when the method is not accessed for the particular operation in the application. It comes under the unchecked exception.
ex
import java.util.*;

public class Example14 {
public static void main(String[] args) {
List list = new ArrayList<>();
Iterator it = list.iterator();
it.remove();
}
}

Tobe discuss : 6,7,8,13,14,

reffer:
https://www.geeksforgeeks.org/java/types-of-exception-in-java-with-examples/

Top comments (0)