Java Exception Handling Interview Questions
1 which package contains exception handling related classes?
Ans: java. Lang
2 what are the two types of Exceptions?
Ans: Checked Exceptions and Unchecked Exceptions.
3 what is the base class of all exceptions?
Ans: java.lang.Throwable
4 what is the difference between Exception and Error in java?
Ans: Exception and Error are the subclasses of the Throw able class. Exception class is used for exceptional conditions that user program should catch. Error defines exceptions that are not excepted to be caught by the user program. Example is Stack Overflow.
5 what is the difference between throw and throws?
Ans: throw is used to explicitly raise a exception within the program, the statement would be throw new Exception (); throws clause is used to indicate the exceptions that are not handled by the method. It must specify this behavior so the callers of the method can guard against the exceptions. Throws is specified in the method signature. If multiple exceptions are not handled, then they are separated by a comma. The statement would be as follows: public void do something () throws IOException, MyException {}
6 Differentiate between Checked Exceptions and Unchecked Exceptions?
Ans: Checked Exceptions are those exceptions which should be explicitly handled by the calling method. Unhandled checked exceptions results in compilation error.
Unchecked Exceptions are those which occur at runtime and need not be explicitly handled. Runtime Exception and its subclasses, Error and its subclasses fall under unchecked exceptions.
7 what are User defined Exceptions?
Ans: Apart from the exceptions already defined in Java package libraries, user can define his own exception classes by extending Exception class.
8 what is the importance of finally block in exception handling?
Ans: Finally block will be executed whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally block will be executed. Finally is used to free up resources like database connections, IO handles, etc.
9 Can a catch block exist without a try block?
Ans: No. A catch block should always go with a try block.
10 Can a finally block exist with a try block but without a catch?
Ans: Yes. The following are the combinations try/catch or try/catch/finally or try/finally.
11 what will happen to the Exception object after exception handling?
Ans: Exception object will be garbage collected.
12 The subclass exception should precede the base class exception when used within the catch clause. True/False?
Ans: True.
13 Exceptions can be caught or rethrown to a calling method. True/False?
Ans: True.
14 The statements following the throw keyword in a program are not executed. True/False?
Ans: True.
15 How does finally block differ from finalize () method?
Ans: Finally block will be executed whether or not an exception is thrown. So it is used to free resoources. Finalize () is a protected method in the Object class which is called by the JVM just before an object is garbage collected.
16 what are the constraints imposed by overriding on exception handling?
Ans: An overriding method in a subclass May only throw exceptions declared in the parent class or children of the exceptions declared in the parent class.