Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
These are most frequently asked interview question from Java Exception handling
Alternate definition: any line of code that could possibly throw exception, and if raised Alternate definition: any line of code that could possibly thr
during compilation is said to be checked exception be unchecked exception
Example of unchecked-exception :
Except Runtime exception & its child classes and error & its child classes, all other exception Runtime exception & its child classes
fall under the category of Checked Exception error & its child classes
Some of the checked exception Some of the unchecked exception
IOException RuntimeException
SQLException NullPointerException
InterruptedException ArithmeticException
Etc etc
When a method catches exception and doesn’t want to handle, instead it want to
propagate exception to caller-method (basically delegating the responsibly to caller
method)
Sometimes, method catches one exception-type and convert to another exception-type
before throwing using throw clause
It is also used to add some user message to caught exception before re-throwing to
caller-method
Note: in all cases, it the responsibility of the caller method to handle this exception
whether by surrounding with try-catch or declare throws clause
Section H) Others:
Q) Explain rules for exception handling with respect to method overriding ?
Read rules for Exception handling w.r.t Method Overriding in Java for more details
Below listed are the rules for exception handling when overriding,
Rule 1: If parent-class method doesn’t declare any exception,
1. Then child-class overriding-method can declare any type of unchecked-exception (this is
the only possibility)
2. If child-class overriding-method declares checked-exception, then compiler
throws compile-time error stating “Exception <exception-type> is not compatible with
throws clause in ParentClass.testMethod()”
3. Then child-class overriding-method can declare no exception (this is very much same as
that of overridden-method of parent-class –> exactly same method signature)
Rule 2: If parent-class method declares unchecked exception,
1. Then child-class overriding-method can declare any type of unchecked-exception (not
necessarily same exception as that of parent-class’ method)
2. If child-class overriding-method declares any checked-exception, then compiler
throws compile-time error stating “Exception <exception-type> is not compatible with
throws clause in ParentClass.testMethod()”
3. Then child-class overriding-method can declare no exception
Rule 3: If parent-class method declares checked exception,
1. Then child-class overriding-method can declare any type of unchecked-exception
2. Then child-class overriding-method can declare same type of checked-exception or one of
its sub-class or no exception
3. Then child-class overriding-method can declare no exception
Rule 4: If parent-class method declares both checked & unchecked exceptions,
1. Then child-class overriding method can declare any type of unchecked-exception
2. Then child-class overriding-method can declare same type of checked-exception or one of
its sub-class or no exception
3. Then child-class overriding-method can declare no exception
Q) What are unreachable block in Java ?
There are various scenarios when this compile-time error is encountered