Java's finally used to do?

We know that if a program language has no garbage collection and destructor automatically calls the mechanism, then, this time finally it is very important.

But for Java, it has garbage collection mechanism, the release of memory is no longer a problem; but no destructors in Java can be called, then Java under what circumstances it would use finally?

finally use in Java?

If you want to restore the memory resources in addition to their initial state, which requires finally clause. These resources include the need to clean up:

Open file or network connection, the graphics on the screen, even when we say that a switch to the outside world.

To demonstrate with an example?

Purpose of this procedure is to ensure that the main () function ends, the switch must be closed, so this way, we are at the end of each of the try block and exception handlers were added to sw.off () method It calls.

However, an exception is thrown in, but it was not trap handler, this is the method 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();
		}
	}
}

operation result:

To solve these problems, use finally improve:

This ensures 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 he said: In the case of exception is not caught in the current exception handler, the exception handling mechanism will be before the jump to a higher level exception handler, executed finally clause:

In this case, when it comes to break and continue statements, finally clause is also executed; however be noted that:

If we break finally and tagged and continue with the use, in Java there is no need to use a 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");
		}
	}
}

 

Guess you like

Origin blog.csdn.net/qq_41026809/article/details/92579748