129.
•
•
•
130.
Answer:
Any code that needs to be always executed irrespective of
whether an exception occurs or not, should be placed within the
finally clause. So, for example clean–up
code like closing
database connections or files on the file system or other
resources can be placed in the finally clause. If such code is
placed in the try block, there is a
possibility that it may not
execute if an exception occurs. So, to ensure that such code is
always executed, it needs to be placed in the finally block.
Explain the differences between Error and Exception.
Answer:
The java.lang.Throwable is the superclass of all the
exception classes. It has two sub–classes, Exception and Error.
There are several differences between the two:
An Exception represents programmatic
errors that are caused
by the application itself like accessing a null value, dividing by
0, error while writing to a file, etc.
An Error represents errors
that occur outside your code within the runtime environment
like running out of memory, etc.
Exceptions are conditions that an application can handle and
recover from. Errors on
the other hand are abnormal
conditions that indicate serious problems that an application
cannot recover from.
Exceptions are both checked as well as unchecked. Checked
exceptions need to be handled or specified within the throws
clause in the method declaration.
Errors on the other hand are
always unchecked and need not be declared in the throws
clause.
Do'stlaringiz bilan baham: