The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught. It is usually used in places where we are connecting to a database so that, we can close the connection or perform any cleanup even if the query execution in the try block caused an exception.

7. What classes of exceptions may be caught by a catch clause?
A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types.

8. Can an exception be rethrown?
Yes, an exception can be rethrown any number of times.

9. When is the finally clause of a try-catch-finally statement executed?
The finally clause of the try-catch-finally statement is always executed after the catch block is executed, unless the thread of execution terminates or an exception occurs within the execution of the finally clause.

10. What classes of exceptions may be thrown by a throw statement?
A throw statement may throw any expression that may be assigned to the Throwable type.

11. What happens if an exception is not caught?
An uncaught exception results in the uncaughtException() method of the thread’s ThreadGroup being invoked, which eventually results in the termination of the program in which it is thrown.

12. What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement?The exception propagates up to the next higher level try-catch statement (if any) or results in the program’s termination.

13. Can try statements be nested?
Try statements can be tested. It is possible to nest them to any level, but it is preferable to keep the nesting to 2 or 3 levels at max.

14. How does a try statement determine which catch clause should be used to handle an exception?
When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception that was thrown, is executed. The remaining catch clauses are ignored.

15. What is difference between error and exception
Error occurs at runtime and cannot be recovered, Outofmemory is one such example. Exceptions on the other hand are due conditions which the application encounters, that can be recovered such as FileNotFound exception or IO exceptions

16. What is the base class from which all exceptions are subclasses ?
All exceptions are subclasses of a class called java.lang.Throwable

17. How do you intercept and control exceptions ?
We can intercept and control exceptions by using try/catch/finally blocks.

You place the normal processing code in try block
You put the code to deal with exceptions that might arise in try block in catch block
Code that must be executed no matter what happens must be place in finally block

18. When do we say an exception is handled ?
When an exception is thrown in a try block and is caught by a matching catch block, the exception is considered to have been handled. Or when an exception thrown by a method is caught by the calling method and handled, an exception can be considered handled.


Please enter your comment!
Please enter your name here