Difference between RuntimeException and checked Exception in Java

RuntimeException vs Checked Exception in Java
Java Exceptions are divided in two categories RuntimeException also known as unchecked Exception and checked Exception. Main difference between RuntimeException and checked Exception is that, It is mandatory to provide try catch or try finally block to handle checked Exception and failure to do so will result in compile time error, while in case of RuntimeException this is not mandatory. Difference between checked and unchecked exception is one of the most popular question on Java interview for 2 to years experienced developer especially related to Exception concepts. Answer to this question is rather similar as mentioned in previous lines and they are mostly asked along with other Java Exception interview questions like difference between throw and throws and Error vs Exception. Any Exception which is subclass of RuntimeException are called unchecked and mandatory exception handling is not requirement for them.

Some of the most common Exception like NullPointerException, ArrayIndexOutOfBoundException are unchecked and they are descended from java.lang.RuntimeException. Popular example of checked Exceptions are ClassNotFoundException and IOException and that's the reason you need to provide a try catch finally block while performing file operations in Java as many of them throws IOException

Similarly many utilities of Reflection API throws java.lang.ClassNotFoundException. In this Java tutorial we will see some more difference between RuntimeException and checked Exception in Java.

Runtime Exception vs Checked Exception in Java

Apart from fundamental difference between Runtime and checked exception, another burning question is while creating custom Exception should you make them unchecked by deriving from java.lang.RuntimeException or checked? well this decision is purely yours though some thoughts are available in Java community. I mostly see JDK when in doubt and try to follow practices available in JDK. 

If a method is likely to fail and chances of failure is more than 50% it should throw Checked Exception to ensure an alternate processing in case it failed. Another thought is that programming errors should be unchecked and derived from RuntimeException e.g. java.lang.NullPointerException. Checked Exception also enforce proper handling of error condition , though its theoretical in nature and many program simply appease compiler by providing try catch block instead of correctly handling exception in catch block. 

One disadvantage of checked exception over runtime exception is that it makes your code ugly by adding boiler plate code in form of try catch finally block. Though this issue is addressed at some extent by improved Exception handling in JDK 7 by introducing automatic resource management or ARM blocks and allowing to catch multiple Exception in same catch block.

RuntimeExcpetion vs Checked Exception in JavaThat's all on difference between runtime exception and checked in Java. this question can also be asked as checked vs unchecked exception. Unchecked means compiler doesn't check and Checked means compiler checks for exception handling.

Other Java Interview questions you may like

1 comment:

  1. Haha! Bad, very bad answer. The try/catch is not mandatory at all. It is mandatory to do something with the checked exception: EITHER try/catch it OR declare it in throws clause.
    The same is true even for the main(String[]) methods. Period. Don't confuse people.