Tag: Checked exception

The reality of Checked v/s Unchecked exceptions in Java

Hi,

I actually did not want to write an article about this topic due to the in-numerous no of posts that we can find in the internet but what motivated me to write down is that i have noticed all the links give almost the same meaning to the end user.

“A checked exception is performed at compile-time and is required to have a try/catch or a throws block in a method.
An unchecked exception is derived from RunTimeException class and is not required to be coded in a try/catch or throws block.”

 

Well, there is nothing wrong in these explanations; they are perfectly valid. But the interesting stuff here is while developing an application which one you should go for? Unless you have a clear understanding of the two types of exceptions you cannot make this choice. The Sun’s documentation page provides below answer for this question:

“If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.”

 

In my opinion we can write code to recover from any kind of exception. 🙂 Nah…i am just trying to be optimistic. For some exceptions we cannot do anything like – no memory left, no disk space left, etc. But my point being is that we can have code to check for null values, array index values, file availability, DB connection availability, etc i.e., we can handle almost all kinds of exceptions. Although the end result is that your application size would be of a greater size than what you have assumed but that is another story of discussion.

 

Let’s see what is a checked and an unchecked exception with an example.

Checked exception –
A check for a resource is called a checked exception. This check is performed by the compiler. For e.g., check for a file, connect to a DB, start a thread, etc. Compiler does not literally checks for the resource during compilation but it expects that you have corresponding piece of code (try/catch) in your application to handle the resource unavailability.

Unchecked exception –
A problem faced by the JVM during the execution of your program i.e, due to wrong values in the variables of an object – NumberFormatException, Array index out of bounds exception, etc.

 

Let us understand this more with an example. Below is a task for which you are requested to write code with proper exception handling. Assume that the functionality given in each of the below is written inside a different method.

  1. Create and start 5 threads.
  2. In the file system, each thread should locate a particular file.
  3. Open the file and read its contents into a List or an array. (For simplicity, we assume the file contains integer numbers)
  4. Loop the array and print the file contents i.e., integer values.

 

The below explains which of the two types of exception handlers you should use.

  • The functionality in #1 requires you to write “thread.start()” which means you have created a new thread and starting it. To java this means, the new thread is a resource for which it will create stack, registers, memory segment, etc. It is a fact that resources may not be available in real-world situations. So compiler enforces the programmer that he/she has extra code written in place to handle this situation and hence this is a compile-time checking i.e, you have to write a checked exception for #1.
  • In #2, the thread will locate a file in the file system. Again we are talking about a resource which is a file and hence you are forced to write a checked exception.
  • In #3, it again is dealing with resources; a checked exception is required
  • In #4, while looping over the values of the array/list, there is a possibility that your code might loop beyond the array’s limit. Even though the syntax may be correct, this kind of problem is something related to the way program has been written i.e., the way you are handling the code. This kind of a problem cannot be anticipated by the compiler. Hence you need to write an un-checked exception for #4.

“While developing custom exception handlers, use checked exceptions to check for resources existence/non-existence and unchecked exceptions to check variables (to be more specific values inside the variables)”

 

If you have more real-world examples please do share and provide your comments. Kindly refer the following links for more information about this topic.

http://tutorials.jenkov.com/java-exception-handling/checked-or-unchecked-exceptions.html

http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

 

Thanks a lot!