Sponsored Links

Ad by Google
Very common and popular java interview question, if you are preparing for java,j2ee interview than this question must be in your plates before going for an interview. Although, I have already posted Top 20 Exception Handling interview questions and posting this question in separate post because this is one of the very respected java exception handling interview question.
If you are preparing for product based company's interview than, I suggest you to please read  15 core java written test interview questions.

Below are the few differences between Checked and Unchecked Exception -
  1. The Checked Exception are those exception which are forced by java compiler to catch it or throws it a compile time, whereas Unchecked Exception are bug(s),logical error(s) in your code, so it can be only identified(occur) at Runtime.
  2. In java all Exceptions are checked exception, except java.lang.Error or it's sub classes and java.lang.Runtime or it's sub classes.
  3. From this image, you can easily identified the Exception hierarchy which one belongs from java.lang.Error and which one belongs from java.lang.Runtime class.
    If you are new to Exception, than I request you to please read the java Exception Tutorial.
  4. Your  code cannot recover from Unchecked Exception,but you can easily recovered from Checked Exception because it occurs at compile time, without resolving checked exception your program will not compile successfully.
  5. Below are the few examples of Checked Exception, most common to see these errors in day to day programming.
    • IOException
    • FileNotFoundException
    • DataFormatException
    • SQLException
    • TimeoutException
    • InterruptedException
  6. Below are the few examples of Unchecked Exception -
    • ArithmeticException
    • ArrayStoreException
    • ClassCastException
    • NullPointerException
    • IndexOutOfBoundsException
    • StackOverflowError
    • NoClassDefFoundError
    • OutOfMemoryError 
  7. Below is the code sample to generate Unchecked Exception -
  8. public static void main(String[] args) {
      List list1 = new ArrayList<>();
      List list2 = new ArrayList<>();
    OUT PUT: Exception in thread "main" java.lang.StackOverflowError at java.util.ArrayList$Itr.next(ArrayList.java:791) at java.util.AbstractList.equals(AbstractList.java:521) at java.util.AbstractList.equals(AbstractList.java:523) at java.util.AbstractList.equals(AbstractList.java:523) at java.util.AbstractList.equals(AbstractList.java:523)
  9. Below is the code sample to generate Checked Exception -
  10. public static void readFie(String fileName) {
    		File file = new File(fileName);
    		try {
    			FileReader fr = new FileReader(file);
    		} catch (FileNotFoundException e) {

Note - You can create custom exception by extending both Checked or Unchecked exception, but if your client( who is going to use your custom exception) can reasonably be expected to recover from an exception than make it checked exception. Otherwise, if your client is not going to do anything to recover from an exception than simply make it Unchecked Exception.

That's it :)

Please write your comments, or if you have anything to add on this.

Sponsored Links


Post a Comment