Transferred from: http://www.cnblogs.com/haitao-fan/archive/2012/05/31/2528389.html
The finally keyword is the best complement to the Java exception-handling model compared to other language models. The finally structure makes the code always executes, regardless of whether or not an exception occurs. Use finally to maintain the internal state of an object and to clean up non-memory resources. If you don't have a finally, your code will be confusing. For example, the following code illustrates how you must write code to free non-memory resources without using finally:
Import java.net.*;
Import java.io.*;
Class withoutfinally
{
public void Foo () throws IOException
{
Create a socket on any of the free ports
ServerSocket ss = new ServerSocket (0);
Try
{
Socket socket = ss.accept ();
The other code here ...
}
catch (IOException E)
{
Ss.close (); 1
Throw e;
}
//...
Ss.close (); 2
}
}
This code creates a socket and calls the Accept method. You must close this socket before exiting the method to avoid resource vulnerabilities. To complete this task, we call close at//2, which is the last statement of the method. But what happens if an exception occurs in a try block? In this case, the close call at//2 never occurs. Therefore, you must catch this exception and insert another call to close at//1 before you re-emit the exception. This will ensure that the socket is closed before exiting the method.
It is cumbersome and error-prone to write code, but it is essential in the absence of a finally. Unfortunately, in languages that do not have a finally mechanism, programmers may forget to organize their code in this way, leading to resource vulnerabilities. The finally clause in Java solves this problem. With finally, the preceding code can be rewritten in the following form:
Import java.net.*;
Import java.io.*;
Class withfinally
{
public void Foo2 () throws IOException
{
Create a socket on any of the free ports
ServerSocket ss = new ServerSocket (0);
Try
{
Socket socket = ss.accept ();
The other code here ...
}
Finally
{
Ss.close ();
}
}
}
The finally block ensures that the Close method is always executed, regardless of whether an exception is emitted within the try block. Therefore, you can ensure that the Close method is always called before exiting the method. This allows you to be confident that the socket is closed and that you are not leaking resources. There is no need for another catch block in this method. The catch block is provided in the first example just to close the socket, which is now closed by finally. If you do provide a catch block, the code in the finally block executes after the catch block finishes.
The finally block must be used in conjunction with a try or Try/catch block. In addition, it is not possible to exit the try block without executing its finally block. If a finally block exists, it is always executed. (from that point of view, the statement is correct.) There is a way to exit a try block without executing a finally block. If the code executes a system.exit (0) inside a try; Statement, the application terminates without performing a finally execution. On the other hand, finally will not execute if you unplug the power during the try block execution. )
[Reprint]java The use of the Finally keyword