Finally
Finally: Finally is a block always associated with try-catch to maintain clean-up code. The speciality of the finally block is that it will be executed irrespective of whether an exception is raised or not raised and whether it is handled or not handled.
Syntax:
try {
risky code
} catch(Exception e) {
throw exception
} finally {
clean-up activity
}
Final
Final: Final is a modifier applicable for classes, methods, and variables.
- If a class is declared as final, we can't extend that class (i.e., we can't create child classes for that class; inheritance is not possible for final classes).
- If a method is final, we can't override that method in the child class.
- If a variable is declared as final, then we can't perform reassignment for that variable.
Finalize()
Finalize(): Finalize is a method always invoked by the garbage collector just before destroying an object to perform clean-up activities. Once the finalize method completes, the garbage collector immediately destroys the object.
Note:
- The finally block is responsible for performing clean-up activities related to the try block (i.e., whatever resources we open as part of the try block will be closed inside the finally block).
- The finalize method is responsible for performing clean-up activities related to the object (i.e., whatever resources are associated with the object will be deallocated before destroying the object by using the finalize method).
Various Possible Combinations of try-catch-finally
| Valid Combination 1 | Valid Combination 2 | Invalid Combination 1 |
|---|---|---|
|
|
Compile time error: duplicate catch blocks |
| Valid Combination 3 | Valid Combination 4 | Invalid Combination 2 |
|
|
Compile time error: try without catch or finally |
| Valid Combination 5 | Invalid Combination 3 | Invalid Combination 4 |
|
Compile time error: catch without try |
Compile time error: finally without try |
| Invalid Combination 5 | Invalid Combination 6 | Invalid Combination 7 |
Compile time error: catch without try |
Compile time error: catch without try |
Compile time error: finally without try |
| Valid Combination 6 | Valid Combination 7 | Valid Combination 8 |
|
|
|
| Valid Combination 9 | Valid Combination 10 | Invalid Combination 8 |
Compile time error: finally without try |
Compile time error: finally without try |
Not Allowed: Curly braces are mandatory |
| Invalid Combination 9 | Invalid Combination 10 | Invalid Combination 11 |
Not Allowed: Curly braces are mandatory |
Not Allowed: Curly braces are mandatory |
Points:
- The order of try-catch-finally is important.
- Whenever writing try, we must write either catch or finally. Otherwise, we will get a compile-time error. That is, try without catch or finally is invalid.
- Whenever we are writing a catch block, a try block must be present; that is, catch without try is invalid.
- Whenever we are writing a finally block, a try block must be present; that is, finally without try is invalid.
- Inside try, catch, and finally blocks, we can declare try-catch-finally blocks. That is, nesting of try-catch-finally is allowed.
- For try, catch, and finally blocks, curly braces are mandatory.