Difference between Checked vs Unchecked Exception in Java? Example (original) (raw)

Checked and Unchecked Exception is two types of Exception exist in Java. Though there is no difference in functionality and you can very achieve the same thing with either checked Exception or Unchecked Exception, there is some difference on exception handling part. In this Java tutorial we will see what is checked and Unchecked Exception in Java, Examples of Checked and Unchecked Exception and most importantly we will learn when to use Checked Exception and when to use Unchecked Exception in Java and lastly we will see the difference between checked and unchecked exception to understand things better.

Difference between Checked vs Unchecked Exception in Java

Now, let's do deep dive on both checked and unchecked Exception classes in Java to understand the difference between them.

1. What is Checked Exception in Java?

Checked Exception in Javais all those Exception which requires being catches and handled during compile time. If Compiler doesn’t see try or catch block handling a Checked Exception, it throws Compilation error. Now Which Exception is checked Exception and Why Checked Exception are introduced in first place?

All the Exception which are direct sub Class of Exception but not inherit RuntimeException are Checked Exception.

While doing File Programming in C++ I found that most of the time programmer forgets to close file descriptors , which often result in locking of file on OS level. Since Java is introduced after C++, designers of Java thought to ensure such mistakes are not allowed and resources opened are closed properly.

To ensure this they introduced Checked Exception. If you see most of File IO related operation comes under IOException which is checked one. Though is a special scenario related to Checked Exception but you can generalize this as, where Java sees an opportunity of failure more, they ensure that programmer provide recovery strategy or at least handle those scenario gracefully.

Since a picture is worth 1000 words I have put together Exception hierarchy in mind map which clearly says which Exceptions are checked and which Exceptions are unchecked.

Difference between Checked and Unchecked Exception in Java

When to use Checked Exception in Java?

Checked Exception vs Unchecked Exception in Java exampleKnowing Checked Exception is not that useful until you know how to use Checked Exception in Java. Java has often been criticized for its Checked Exception strategy, arguments given are that checked Exception adds a lot of boilerplate code and makes the whole class or function unreadable.

Somewhat I agree with this and java also recognizes this by introducing improved Exception handling mechanism in Java7 but Checked Exception does have its real purpose. Following are some scenarios where I would prefer to use Checked Exception to ensure that Code is Robust and stable:

  1. All Operation where chances of failure is more e.g. IO Operation, Database Access or Networking operation can be handled with Checked Exception.

  2. When you know what to do (i.e. you have an alternative) when an Exception occurs, may be as part of the Business Process.

  3. Checked Exception is a reminder by compiler to programmer to handle failure scenario.

3. Example of checked Exception in Java API

Following are some Examples of Checked Exception in Java library:

IOException

DataAccessException

InvocationTargetException

4. What is an Unchecked Exception in Java?

Unchecked Exception in Java is those Exceptions whose handling is not verified during Compile time. Unchecked Exceptions mostly arise due to programming errors like accessing the method of a null object, accessing elements outside an array bonding, or invoking methods with illegal arguments.

In Java, Unchecked Exception is a direct sub Class of RuntimeException. What is a major benefit of Unchecked Exception is that it doesn't reduce code readability and keeps the client code clean.

5. When to use UncheckedException in Java?

A good strategy of Exception handling in Java is wrapping a checked Exception into UnCheckedException. Since most of the Database operation throws SQLException but it’s not good to let SQLException propagate from your DAO layer to up higher on business layer and client code provide exception handling you can handle SQLException in DAO layer and you can wrap the cause in a RuntimeException to propagate through client code.

Also as I said earlier unchecked exceptions are mostly programming errors and to catch them is really hard until you do a load test with all possible input and scenarios. See these free Core Java online courses for more details.

Difference between Checked and Unchecked Exception in Java

Now we have enough information to differentiate Checked Exception with Unchecked Exception:

  1. Checked Exception is required to be handled by compile-time while Unchecked Exception doesn't.

  2. Checked Exception is a direct sub-Class of Exception while Unchecked Exception are of RuntimeException.

  3. CheckedException represents a scenario with a higher failure rate while UnCheckedException is mostly programming mistakes.

7. Example of unchecked Exception in Java API

Here are few examples of Unchecked Exception in Java library:

8. Summary:

1. Both Checked and Unchecked Exception are handled using keyword try, catch, and finally.

2. In terms of Functionality Checked and Unchecked Exception are the same.

3. Checked Exception handling verified during compile time.

4. Unchecked Exception is mostly programming errors

Related Java Tutorials