16) Is finally block executed even if there is a return statement in the try block ?
Yes, finally block will be executed even if there is a return statement in the try block.
17) Is it mandatory to use the finally block ?
It is always a good practice to use the finally block. The reason for using the finally block is, it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break statements. For example In finally block we can close connection object.
18) Is an empty catch block legal ?
Yes an empty catch block is legal. We can have the catch block without writing any actual code to handle the exception caught.
19) What is a throw keyword in Java ?
The throw keyword in Java is used to explicitly throw an exception. We can throw either checked or uncheked exception in java by throw keyword. It is mainly used when the program fails to satisfy the given condition and it wants to warn the application. The exception thrown should be subclass of Throwable.
20) Can a catch block throw an exception caught by itself ?
Yes. This is called rethrowing of the exception by catch block.
21) Can a catch or finally block throw exception in java ?
Yes, catch or finally block can throw checked or unchecked exception but it must be handled accordingly.
22) Is it possible to write multiple exceptions in single catch block ?
This new feature is added in java 7. From Java 7 onwards you can use this feature.
23) What is rethrowing an exception in java ?
The Exceptions that occur in the try block are handled in the catch block. If the catch block is unable to handle the exception, it can rethrow that exception using throw keyword. It is called rethrowing an exception.
24) What is a throws keyword in Java ?
Throws keyword in Java is used to declare that a method may throw one or more exceptions. The caller of this method has to catch these exceptions. The catching of these exceptions is optional if the exceptions are of type unchecked exceptions or error. If the exceptions are not caught, a compile time error will be thrown. throws keyword..
25) When to use throws keyword ?
When you don’t want to handle the exceptions in the method using try-catch block and want the exception to be handled by calling method, then declare the exceptions using the throws keyword in method signature.
26) What is the difference between throw and throws keyword ?
|throw keyword is used to throw Exception from any method or static block in Java.||throws keyword is used in the method signature to declare the exceptions that are thrown by the method.|
|Throw keyword is used within the method.||Throws keyword is used with the method signature.|
|You cannot throw multiple exceptions.||You can declare multiple exceptions in method signature using throws clause.|
27) Can we override a super class method which is throwing an unchecked exception with checked exception in the sub class ?
No. If a super class method is throwing an unchecked exception, then it can be overridden in the sub class with same exception or any other unchecked exception but can not be overridden with checked exception.
28) When we override a method in a Child class, can we throw an additional Exception that is not thrown by the Parent class method ?
Yes, Java allows us to throw additional Exception in a child class, but the additional exception should be an unchecked exception i.e. RuntimeException.
29) What is the difference between ClassNotFoundException and NoClassDefFoundError ?
A ClassNotFoundException is thrown when the reported class is not found by the ClassLoader in the CLASSPATH. ClassNotFoundException is a checked exception in Java.
NoClassDefFoundError is thrown when JVM tries to load a class which is available at compile time but not available at runtime. NoClassDefFoundError is an Error in Java.
30) Can we overload a method on the basis of exceptions in java ?
Yes, we can overload a method on the basis of exceptions.