Exception Handling
Exception Handling
The finally
block always executes when the try block exits. So you can use finally without catch
but you must use try.
The reason why you cannot have a finally without a try is because you could have
multiple finally statements in the same scope and the try indicates what block of code
the finally pertains to in case an error occurs.
Another interesting feature of the finally is that it must execute no matter what when
the try is entered. For example what if you use a goto to skip over
your finally statement? If the goto is inside of the try it will execute
the finally statement however if the goto is above/outside the try statement then it will
skip the finally code. finally is relevant only to the code that is surrounded in the try. If
you have no try then the finally is not relevant to anything.
The code within the finally block is guaranteed to execute if program control flow enters
the corresponding try block. So it makes no sense to have a finally without a try.
The only exception to this is if the program encounters a System.exit(...) call before
the finally block, as that shuts down the virtual machine.
What statements can exist in between try, catch and finally blocks?
No, try, catch and finally forms a single unit and no other statements should
exist in between try, catch and finally blocks
Are we allowed to use only try block without a catch and finally blocks?
Prior to Java 7:
No, it is not allowed. If used it shows compilation error. The try block must be
followed by a catch block or finally block.
final keyword:
By declaring a variable as final, the value of final variable cannot be changed.
By declaring a method as final, method cannot be overridden.
By declaring a class as final, class cannot be extended.
finally:
Used after try or try-catch block, will get executed after the try and catch
blocks without considering whether an exception is thrown or not.
finalize:
Finalize method is the method that Garbage Collector always calls just before
the deletion/destroying the object which is no longer in use in the code
finally block will always be executed except one scenario as discussed above
in Q3. By ensuring the cleanup operations in finally block, you will assure that
those operations will be always executed irrespective of whether an exception
has occurred or not.
Checked exceptions are also caught during runtime only. But there are certain
exceptions which the JVM can't decide what action to be taken when they occur. So,
these are categorized into checked exceptions. The user has to mention in prior
what action to be taken if these occur. That's why Checked.
BASIS FOR
CHECKED EXCEPTION UNCHECKED EXCEPTION
COMPARISON
Basic The compiler checks the checked exception. The compiler does not check the
BASIS FOR
CHECKED EXCEPTION UNCHECKED EXCEPTION
COMPARISON
Unchecked exception.
Class of Except "RuntimeException" class all the child "RuntimeException" class and its
Exception classes of the class "Exception", and the child classes, are"Unchecked
Checked Exception.
Handling If we do not handle the checked exception, Even if we do not handle the
doesn't object.
Compilation The program doesn't compile if there is an The program compiles successfully
code.
THROW THROWS
throw. name.
1. If SuperClass does not declare an exception, then the SubClass can only
declare unchecked exceptions, but not the checked exceptions.
2. If SuperClass declares an exception, then the SubClass can only declare the
child exceptions of the exception declared by the SuperClass, but not any
other exception.
3. If SuperClass declares an exception, then the SubClass can declare without
exception.