Checked and Unchecked Exception is two types of Exception exist in Java. Though there is no difference in functionality and you can very achieve 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 difference between checked and unchecked exception to understand things better. By the way this article is second in my post on Exception along with difference between throw and throws in Java and improved Exception handling in Java 7 with Automatic resource management ARM and multi-catch block in JDK 7.
Difference between Checked vs Unchecked Exception in Java
What is Checked Exception in Java?
Checked Exception in Java is 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.
When to use Checked Exception in Java
Knowing 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 lot of boiler plate code and makes whole class or function unreadable. Somewhat I agree with this and java also recognize 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 alternative) when an Exception occurs, may be as part of Business Process.
3) Checked Exception is a reminder by compiler to programmer to handle failure scenario.
Example of checked Exception in Java API
Following are some Examples of Checked Exception in Java library:
IOException
DataAccessException
InvocationTargetException
What is 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 method of a null object, accessing element outside an array bonding or invoking method with illegal arguments. In Java, Unchecked Exception is direct sub Class of RuntimeException. What is major benefit of Unchecked Exception is that it doesn't reduce code readability and keeps the client code clean.
When to use UnCheckedException in Java
A good strategy of Exception handling in Java is wrapping a checked Exception into UnCheckedException. Since most of 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 real hard until you do a load test with all possible input and scenario.
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 direct sub-Class of Exception while Unchecked Exception are of RuntimeException.
3) CheckedException represent scenario with higher failure rate while UnCheckedException are mostly programming mistakes.
Example of unchecked Exception in Java API
Here are few examples of Unchecked Exception in Java library:
NullPointerException
ArrayIndexOutOfBound
IllegalArgumentException
IllegalStateException
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 same.
3. Checked Exception handling verified during compile time.
4. Unchecked Exception are mostly programming errors
5. JDK7 provides improved Exception handling code with catching multiple Exception in one catch block and reduce amount of boiler plate code required for exception handling in Java.
Related Java Tutorials
Tidak ada komentar:
Posting Komentar