Finally, the
is particularly important in a language where there is no garbage collection and an ⑤ mechanism (annotation), because a programmer can use it to guarantee the correct release of memory-whatever happens inside a try block. But Java provides a garbage collection mechanism, so the release of memory is almost certainly not a problem. In addition, it does not have a builder to call. In that case, when will the Java use finally?
⑤: "destructor" is the antonym of the Builder (constructor). It represents a special function, which is usually invoked once an object loses its usefulness. We certainly know where and when to call the wreck. C + + provides an automatic mechanism for the destruction of the device, but Delphi version 1 and 2 of the object Pascal do not have this capability (in this language, the meaning and use of the device has changed). Finally,
is required in addition to setting the memory back to its original state to set something else. For example, we sometimes need to open a file or create a network connection, or draw something on the screen, or even set up a switch on the outside world, and so on. As shown in the following example:
: Onoffswitch.java
//Why use finally?
Class Switch {
Boolean state = false;
Boolean read () {return state;}
void on () {state = true;}
void off () {state = false;}
}
public class Onoffswitch {
static switch SW = new switch ();
public static void Main (string[] args) {
try {
sw.on ();
Code that can throw exceptions
... Sw.off ();
} catch (NullPointerException e) {
System.out.println ("NullPointerException");
Sw.off ();
} catch (IllegalArgumentException e) {
System.out.println ("IOException");
Sw.off ();}}
///:~
The goal here is to ensure that the switch is turned off when main () completes, so that the Sw.off () is placed at the end of the try block and each offending controller. But the resulting violation may not have been captured here, and this will miss Sw.off (). However, with finally, we can put the closing code from the try block in one place only:
: Withfinally.java
//Finally guarantees cleanup
class Switch2 {
Boolean state = false;
Boolean read () {return state;}
void on () {state = true;}
void off () {state = false;}
}
public class Withfinally {
static Switch2 sw = new Switch2 ();
public static void Main (string[] args) {
try {
sw.on ();
Code that can throw exceptions ...
} catch (NullPointerException e) {
System.out.println ("NullPointerException");
} catch ( IllegalArgumentException e) {
System.out.println ("IOException");
} finally {
sw.off ();
}} } ///:~
Here, Sw.off () has moved to a place. No matter what happens, it will definitely run.
Even if the violation is not captured in the current catch clause, finally it can be executed before the offending control mechanism goes to a higher level to search for a controller. As shown below:
: Alwaysfinally.java
//Finally is always executed
class Ex extends Exception {} public
class alwaysfinally {public
static void Main (string[] args) {
System.out.println (
"Entering-a-try block");
try {
System.out.println (
"Entering second try block");
try {
throw new Ex ();
} finally {
System.out.println (
"finally in 2nd try block");
} catch (ex e) {
System.out.println (
"caught ex in the In a");
} finally {
System.out.println c22/> "Finally in 1st try Block");}}
///:~
The output of the program shows what happens:
Entering I try block
entering second try blocks
finally in 2nd try blocks
caught Ex in a try Block
finally in 1st try Block
The finally statement can also be executed if the break and continue statements are invoked. Note that, together with the tagged break and continue, finally excludes Java's need for Goto jump statements.