We know , If a program language has no garbage collection and destructor automatic call mechanism , here finally It seems very important.
But for Java Come on , It has a garbage collection mechanism , Memory release is no longer a problem ; and Java There is no destructor in to call , that Java Under what circumstances will it be used finally Well ?
finally stay Java Use in ?
If you want to restore resources other than memory to their original state , This requires finally Clause . These resources that need to be cleaned up include :
Open file or network connection , Graphics on the screen , Even if we say it’s a switch in the outside world .
Use examples to demonstrate ?
The purpose of this procedure is to ensure that main() At the end of the function , The switch must be off , So in this way , We are in each try At the end of both the block and the exception handler, a pair of sw.off() Method call .
But when the exception is thrown , But not captured by the handler , This is the method that will not be called .
public class Switch {
private boolean state = false;
public boolean read() {
return state;
}
public void on() {
state = true;
System.out.println(this);
}
public void off() {
state = false;
System.out.println(this);
}
public String toString() {
return state ? "on":"off";
}
}
public class OnOffException1 extends Exception{
}
public class OnOffException2 extends Exception{
}
public class OnOffSwitch {
private static Switch sw = new Switch();
public static void f()
throws OnOffException1,OnOffException2{}
public static void main(String[] args) {
try {
sw.on();
// Code that can throw exceptions...
f();
sw.off();
}catch(OnOffException1 e) {
System.out.println("OnOffException1");
sw.off();
}catch(OnOffException2 e) {
System.out.println("OnOffException2");
sw.off();
}
}
}
Running results :
To solve the above problems , Use finally Improvement :
That’s the guarantee sw.f() Method will be executed in any case .
public class OnOffSwitch {
private static Switch sw = new Switch();
public static void f()
throws OnOffException1,OnOffException2{}
public static void main(String[] args) {
try {
sw.on();
// Code that can throw exceptions...
OnOffSwitch.f();
}catch(OnOffException1 e) {
System.out.println("OnOffException1");
}catch(OnOffException2 e) {
System.out.println("OnOffException2");
}finally {
sw.off();
}
}
}
Even said : If the exception is not caught by the current exception handler , Exception handling mechanism will also jump to a higher level of exception handler before , perform finally Clause :
under these circumstances , When it comes to break and continue At the time of statement ,finally Clause will also execute ; But here’s the thing :
If we were to finally And labeled break And continue In combination with , stay Java There is no need to use goto Statement .
class FourException extends Exception{}
public class AlwaysFinally {
public static void main(String[] args) {
System.out.println("Entering first try block");
try {
System.out.println("Entering second try block");
try{
throw new FourException();
}finally{
System.out.println("finally in 2nd try block");
}
}catch(FourException e) {
System.out.println("Caught FourException in 1st try block");
}finally {
System.out.println("finally in 1st try block");
}
}
}