- What is
Exception Handling?
Exception Handling is a
mechanism to handle runtime errors. It is mainly used to handle checked
exceptions.
- What is
difference between Checked Exception and Unchecked Exception?
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-time.
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.
- What is the
base class for Error and Exception?
Throwable.
- Is it
necessary that each try block must be followed by a catch block?
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.
- What is
finally block?
finally block is a block
that is always executed.
- Can finally
block be used without catch?
Yes, by try block. finally
must be followed by either try or catch.
- Is there any
case when finally will not be executed?
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).
- Can an
exception be rethrown?
Yes.
- Can subclass
overriding method declare an exception if parent class method doesn't
throw an exception?
Yes but only unchecked
exception not checked.
- What is
exception propagation?
Forwarding the exception
object to the invoking method is known as exception propagation.
- What is
difference between throw and throws?
S.No
|
throw keyword
|
throws keyword
|
1
|
throw is used to explicitly throw an exception.
|
throws is used to declare an exception.
|
2
|
checked exceptions can not be propagated with throw
only.
|
checked exception can be propagated with throws.
|
3
|
throw is followed by an instance.
|
throws is followed by class.
|
4
|
throw is used within the method.
|
throws is used with the method signature.
|
5
|
You cannot throw multiple exception
|
You can declare multiple exception e.g. public void
method()throws IOException,SQLException.
|
No comments:
Post a Comment