Wednesday, August 13, 2008

Exception handling Review Questions




1) Which package contains exception handling related classes?

java.lang

2) What are the two types of Exceptions?

Checked Exceptions and Unchecked Exceptions.

3) What is the base class of all exceptions?

java.lang.Throwable

4) What is the difference between Exception and Error in java?

Exception and Error are the subclasses of the Throwable 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?

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 doSomething() throws IOException,MyException{}

6) Differentiate between Checked Exceptions and Unchecked Exceptions?

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. RuntimeException and it's subclasses, Error and it's subclasses fall under unchecked exceptions.

7) What are User defined Exceptions?

Apart from the exceptions already defined in Java package libraries, user can define his own exception classes by extending Exception class.

Cool What is the importance of finally block in exception handling?

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?

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?

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?

Exception object will be garbage collected.

12) The subclass exception should precede the base class exception when used within the catch clause. True/False?

True.

13) Exceptions can be caught or rethrown to a calling method. True/False?

True.

14) The statements following the throw keyword in a program are not executed. True/False?

True.

15) How does finally block differ from finalize() method?

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?

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.

No comments: