In this article, we will learn about the different types of exceptions in Java checked exceptions, unchecked exceptions, and errors.
Exceptions can happen in any scenario, and all developers would have come across exceptions, for example, whenever we try to read a file that doesn’t exist or whenever we try to read the elements in an array beyond its size and so on.
It is always better to know which exception occurs in which situation so that we will have better control in handling the exception.
Types of Exceptions in Java
Exceptions can be divided into three main categories
- Checked exceptions (Compile-time exceptions)
- Unchecked exceptions (Runtime exceptions)
- Errors
All the exceptions descend from the Throwable class, and then the hierarchy splits into two branches Error and Exception.
Error hierarchy describes the internal error or any resource exhaustion or any other malfunction which happens in the JVM. On the other hand, the Exceptions branch further splits into two IOException and Runtime exceptions.
Except for the Error and RuntimeException and its subclasses, all other classes will be a part of Checked Exception (Compile-time exception).
Checked Exception happens on the occurrence of events which is beyond the control of the JVM like File not present or reading from the database where there might be a situation that the database is down etc.
Whereas Runtime Exception happens due to bad programming such as not handling null properly, dividing a number by zero, etc.
Let’s discuss them in detail
1. Checked Exception or Compile Time Exception:
A Checked Exception or Compile-Time Exception is a subclass of the java.lang.Exception but not a subclass of java.lang.RuntimeException.
Checked Exception is the exception that will be checked during the compile-time, if a method throws a checked exception then the calling method must have one of the below
- A try-catch block to handle the exception
(or)
- Throw the exception using throws keyword in the method signature
A checked exception occurs whenever we are doing some operation that is not in the control of the JVM.
For example, Let’s try to open a file.
package com.javainterviewpoint;
import java.io.FileInputStream;
public class FileRead
{
public static void main(String[] args)
{
FileInputStream fileInputStream = new FileInputStream("test.txt");
}
}
The above code throws a compile-time exception “FileNotFoundException”, as there can be a possibility of file not present at the mentioned location.
When we look at the constructor of FileInputStream,
public FileInputStream(String name) throws FileNotFoundException { this(name != null ? new File(name) : null); }
The declaration says that the above constructor produces the FileInputStream object using the string parameter passed, and in case of any issues, it will throw FileNotFoundException.
In order to make the above code work we need to enclose it in the try-catch block or throw the exception
package com.javainterviewpoint; import java.io.FileInputStream; import java.io.FileNotFoundException; public class FileRead { public static void main(String[] args) { try { FileInputStream fileInputStream = new FileInputStream("test.txt"); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
2. Unchecked Exception or RunTimeException:
A Runtime Exception or Uncheck Exception is a subclass of java.lang.RunTimeException class. Runtime exception usually occurs because of bad programming or programming error.
Since the Unchecked exceptions happen during the run time, we don’t need to throw the exception in the method signature, though we can do it but not mandatory.
For example, NullPointerExecption is a type of RunTimeException which occurs when a variable is not assigned an object and still points to null.
package com.javainterviewpoint;
public class NullTest
{
public static void main(String[] args)
{
String name = null;
System.out.println(name.length());
}
}
The above code clearly shows that the exception occurred due to bad programming. A simple null check before performing any operation on the variable will sort out the issue.
package com.javainterviewpoint; public class NullTest { public static void main(String[] args) { String name = null; if (name != null) System.out.println(name.length()); } }
Like the Checked exception, we can use a try-catch to catch the runtime exception.
package com.javainterviewpoint; public class NullTest { public static void main(String[] args) { String name = null; try { System.out.println(name.length()); } catch (NullPointerException ne) { System.out.println("NullPointerException has occured!!"); } } }
3. Error
An Error is a subclass of java.lang.Error class. The Error indicates a severe issue that cannot be controlled through the code.
For example, OutOfMemoryError occurs when the Java Heap space is full, StackOverflowError is another error which the JVM throws when the stack required for the program is higher than the memory allocated by the JRE.
package com.javainterviewpoint; public class StackOverFlow { public static void main(String args[]) { disp(); } public static void disp() { disp(); } }
The above code eventually throws StackOverFlowError as the disp() method runs infinite number of times.
Exception in thread "main" java.lang.StackOverflowError at com.javainterviewpoint.StackOverFlow.disp(StackOverFlow.java:12) at com.javainterviewpoint.StackOverFlow.disp(StackOverFlow.java:12) at com.javainterviewpoint.StackOverFlow.disp(StackOverFlow.java:12) at com.javainterviewpoint.StackOverFlow.disp(StackOverFlow.java:12) at com.javainterviewpoint.StackOverFlow.disp(StackOverFlow.java:12) at com.javainterviewpoint.StackOverFlow.disp(StackOverFlow.java:12) at com.javainterviewpoint.StackOverFlow.disp(StackOverFlow.java:12)
Tough Error represents a severe issue, and we should not handle it, we can still catch the Error like below.
package com.javainterviewpoint; public class StackOverFlow { public static void main(String args[]) { try { disp(); } catch (StackOverflowError se) { System.out.println("StackOverflowError has occured!!"); } } public static void disp() { disp(); } }
Happy Learning!!
Leave a Reply