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 -
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.
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 -
- 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.
- 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. 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.
- 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.
- 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
- Below are the few examples of Unchecked Exception -
- ArithmeticException
- ArrayStoreException
- ClassCastException
- NullPointerException
- IndexOutOfBoundsException
- StackOverflowError
- NoClassDefFoundError
- OutOfMemoryError
- Below is the code sample to generate Unchecked Exception -
- Below is the code sample to generate Checked Exception -
public static void main(String[] args) { List list1 = new ArrayList<>(); List list2 = new ArrayList<>(); list1.add(list2); list2.add(list1); System.out.println(list1.equals(list2)); }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)
public static void readFie(String fileName) { File file = new File(fileName); try { FileReader fr = new FileReader(file); } catch (FileNotFoundException e) { e.printStackTrace(); } }
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
0 comments:
Post a Comment