Exception Handling Interview Questions
Q -
All exceptions other than those in the RuntimeException class must be either
caught, or declared in a throws clause of any method that can throw them: True
or False? If false, explain why.
A -
True.
Q -
What method of which class would you use to extract the message from an
exception object?
A -
The getMessage() method of the Throwable class.
Q -
Normally, those exception handlers designed to handle exceptions closest to the
root of the exception class hierarchy should be placed first in the list of
exception handlers: True or False? If false, explain why.
A -
False. The above statement has it backwards. Those handlers designed to handle
exceptions furthermost from the root of the hierarchy tree should be placed
first in the list of exception handlers.
Q -
Explain why you should place exception handlers furthermost from the root of
the exception hierarchy tree first in the list of exception handlers.
A -
An exception hander designed to handle a specialized "leaf" object
may be preempted by another handler whose exception object type is closer to
the root of the exception hierarchy tree if the second exception handler
appears earlier in the list of exception handlers.
Q -
In addition to writing handlers for very specialized exception objects, the
Java language allows you to write general exception handlers that handle
multiple types of exceptions: True or False? If false, explain why.
A -
True.
Q -
Your exception handler can be written to handle any class that inherits from
Throwable. If you write a handler for a node class (a class with no
subclasses), you've written a specialized handler: it will only handle
exceptions of that specific type. If you write a handler for a leaf class (a
class with subclasses), you've written a general handler: it will handle any
exception whose type is the node class or any of its subclasses. True or False?
If false, explain why.
A -
False. "Leaf" and "node" are reversed in the above
statement. If you write a handler for a "leaf" class (a class with no
subclasses), you've written a specialized handler: it will only handle
exceptions of that specific type. If you write a handler for a "node"
class (a class with subclasses), you've written a general handler: it will
handle any exception whose type is the node class or any of its
subclasses."
Q -
Java's finally block provides a mechanism that allows your method to clean up
after itself regardless of what happens within the try block. True or False? If
false, explain why.
A -
True.
Q -
Explain how you would specify that a method throws one or more exceptions.
A -
To specify that a method throws one or more exceptions, you add a throws clause
to the method signature for the method. The throws clause is composed of the
throws keyword followed by a comma-separated list of all the exceptions thrown
by that method.
Q -
Provide a code fragment that illustrates how you would specify that a method
throws more than one exception.
A -
See code fragment below.
void myMethod() throws InterruptedException,
MyException,
HerException, UrException
{
//method body
}
Q -
What type of argument is required by the throw statement?
A -
The throw statement requires a single argument, which must be an object derived
either directly or indirectly
from
the class Throwable.
0 comments:
Post a Comment