Thursday, September 26, 2013

When/How should we create a custom checked and unchecked exception in Java ?

Checked Exceptions Represents exceptional scenario which if occurred, must dealt with in some way. example is IOException, FileNotFoundException. We need to declare these exceptions along with the code dealing with such scenarios. Custom checked exceptions can be created by extending your class from java.lang.Exception Class.

Unchecked/Runtime Exceptions Represents an error in our program's logic which can not be reasonably recovered from at run time, for example NullPointerException, ArrayIndexOutOfBoundsException. We do not need to declare/catch such exception in the method signature because these are not expected by any programmer. Custom unchecked exceptions can be created by extending from RuntimeException

Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. A Custom error can be created by extending our class from Throwable.