The exception handling in Java is one of the powerful mechanisms to handle specific errors that arise in a code sequence at runtime. An exception is nothing but a runtime error that disrupts the normal flow of the application.
It normally occurs in a piece of code and when it does, an object representing that exception is created and thrown in the method that caused it. As a result, the program or application is disrupted causing the application to terminate abnormally, which is not recommended. This is why these exceptions are to be handled and this is where the exception-handling mechanism comes to the picture.
There are two types of exceptions that exist in Java – Checked and Unchecked Exception. In this article, we will discuss in detail what is checked and unchecked exception and when to use them, and lastly, we will see the difference between the two.
What is Checked Exception in Java?
These are the exceptions that are checked and handled at compile time. Java defines several exception classes inside the standard package java.lang, and the most common exceptions are subclasses of the standard type RuntimeException.
Those exceptions that occur in areas outside the immediate control of the program are known as checked exceptions. If a method throws a checked exception at the compile time which cannot handle it itself, then it must handle the exception in a catch {} block or declare the exception using the throws keyword, otherwise it will give a compilation error. The compiler examines the exception object’s type for each throw statement and if it shows checked, the compiler checks the source code to make sure the exception is handled in the method where it is thrown or is declared so that it can be handled further.
All exceptions described by Exception which are its direct subclasses except for RuntimeException are called checked exceptions. Now when to use checked exception? You can choose to use an checked exception when you know exactly what to do when an Exception occurs or when the chances of failure is more. Some examples of checked exceptions in Java library include IOException, DataAccessException, IllegalAccessException, InterruptedException, ClassNotFoundException, InvocationTargetException, etc.
What is Unchecked Exception in Java?
These are the exceptions that are not checked at compile time. Unchecked Exception represents a condition that generally reflects errors in a program’s logic and which cannot be recovered from at runtime.
In simple terms, unchecked exception is an exception that isn’t caught in a try-catch block. They don’t need to be caught explicitly. In fact, when an unchecked exception occurs, Java handles the exception automatically. Unchecked exceptions mostly occur due to programming errors such as when accessing element outside an array bonding or accessing method of a null object. In Java, all exceptions under throwable are checked exceptions, whereas exceptions which are direct subclasses of Error and RuntimeException are unchecked exceptions.
The best part of an unchecked exception is that it does not reduce code readability while keeping the client code clean. Some examples of unchecked exceptions include ArithmeticException, ClassCastException, NullPointerException, ArrayIndexOutOfBound, IllegalArgumentException, IllegalStateException, ArrayStoreException, SecurityException, etc.
Difference between Checked and Unchecked Exception
Definition of Checked and Unchecked Exception
Checked and Unchecked Exception are the two types of exceptions exist in Java to ensure normal flow of the application. Both serve the same purpose that is to handle exceptions in Java and though there is no difference in functionality, there is no single rule to determine when to use checked and when to use unchecked exception. Checked Exception is an exception that is checked at compile time, whereas Unchecked Exception is the one that is not checked at compile time.
Basics of Checked and Unchecked Exception
A checked exception refers to the invalid scenarios that occur outside the immediate control of the program whereas unchecked exception refers to the errors in the program or bugs in the program’s logic that cannot be recovered from at runtime.
Exception Handling
If a method throws a checked exception, the compiler will check whether the method is handling that exception using try-catch block or specify the exception using throws keyword. If the method does not provide the handling code, it will show a compilation error. On the contrary, the program compiles just fine because the compiler does not check for Unchecked Exceptions at the compile time.
Cause of Checked and Unchecked Exception
Exceptions that are not derived from RunTimeException are often handled whereas as exceptions occur at runtime are rarely handled. A Checked Exception occurs when the chances of failure are too high. Unchecked Exception, on the other hand, occurs mostly due to programming mistakes. All exceptions under RuntimeException and Error classes are unchecked exceptions, whereas everything else under throwable are checked exceptions.
Examples of Checked and Unchecked Exception
Some examples of checked exceptions are IOException, DataAccessException, IllegalAccessException, InterruptedException, ClassNotFoundException, and more. Examples of unchecked exceptions include NullPointerException, ArithmeticException, ArrayIndexOutOfBound, IllegalArgumentException, IllegalStateException, ArrayStoreException, SecurityException, etc.
Checked vs. Unchecked Exception: Comparison Chart
Summary of Checked and Unchecked Exception
Checked and Unchecked Exception are the two types of exceptions that exist in Java. While the functionality is same for both exceptions, they have their fair share of differences. The main difference between checked and unchecked exceptions is that the former is checked by the compiler whereas the latter escapes the notice of compiler. Exceptions that are checked and handled at compile time are checked exceptions, whereas exceptions that are not checked and handled at compile time are unchecked exceptions. This article explains what is checked and what is unchecked exception, when to use them, and explains the difference between the two.