FileReader fr = new FileReader("file.txt"); // May throw IOException
int result = 10 / 0; // Throws ArithmeticException
String str = null;
System.out.println(str.length()); // Throws NullPointerException
There are two types of Exceptions in Java
try-catch or declared using throws.
IOException
FileReader fr = new FileReader("abc.txt"); // May throw IOException
SQLException
Connection con = DriverManager.getConnection(url, user, pass); // May throw SQLException
ArithmeticException
int result = 10 / 0; // Throws ArithmeticException
ArrayIndexOutOfBoundsException
int[] arr = {10, 20, 30};
System.out.println(arr[5]); // Throws ArrayIndexOutOfBoundsException
Exception is the pre-defined class in Java which inherits the Throwable class.
Exception class in Java.
Object class is the parent class of all the classes in Java
Throwable class is the parent class of Exception class in Java.
Exception class itself is a checked exception, because it is not a subclass of RuntimeException.
catch keyword is used to handle exceptions in Java.
try, finally, throw, throws) have different functionalities and do not directly handle exceptions.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.