There is given a list of exception handling interview questions with answers. If you know any exception handling interview question, kindly post it in the comment section.Question-73: What is Exception Handling?
Answer:
Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.
Question-74: What is difference between Checked Exception and Unchecked Exception?
Answer:
1) Checked Exception:
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time2)Unchecked Exception:
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.Question-75: What is the base class for Error and Exception?
Answer:
Throwable.
Question-76: Is it necessary that each try block must be followed by a catch block?
Answer:
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.
Question-77: What is finally block?
Answer:
finally block is a block that is always executed.
Question-78: Can finally block be used without catch?
Answer:
Yes, by try block. finally must be followed by either try or catch.
Question-79: Is there any case when finally will not be executed?
Answer:
finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
Question-80: What is difference between throw and throws?
Answer:
- throw is used to explicitly throw an exception. throws is used to declare an exception.
- checked exceptions can not be propagated with throw only. checked exception can be propagated with throws.
- throw is followed by an instance. throws is followed by class.
- throw is used within the method. throws is used with the method signature.
- You cannot throw multiple exception You can declare multiple exception e.g. public void method()throws IOException,SQLException.
Question-81:Can an exception be rethrown?
Answer:
Yes.
Question-82: Can subclass overriding method declare an exception if parent class method doesn’t throw an exception ?
Answer:
Yes but only unchecked exception not checked.
Question-83: What is exception propagation ?
Answer:
Forwarding the exception object to the invoking method is known as exception propagation.
No comments:
Post a Comment