Usage of Finally clause
Compared to other model languages, the Finally keyword is the best supplement to the Java exception handling model. The Finally structure has enabled the code to perform, regardless of any abnormal occurrence.
The Java virtual machine calls the subroutines of the Finally clause at each try statement block and the Catch clause associated with it. If there is no exception, or execute Return, Continue, Break, the subroutine of this FINALLY clause performs "return" operation. The program continues to perform the following statement in the first call of the microcontrol.
The JSR and JSR_W instructions are the operation code that makes the Java virtual machine to the miniature subroutine. The difference is that the previous operand supports the number of two bytes, and the last operand supports 4 bytes. When the Java virtual machine enters the JSR or JSR_W instruction, it presses the return address into the stack and continues to execute from the beginning of the miniature subroutine.
After the microcontrol is executed, the RET instruction will be called. The function of the RET instruction is to perform the operation returned from the subroutine. But if Break, Return, Continue, or throw an exception exit FinalLy clause, this RET instruction will not be executed. Because of this, the Java virtual machine starts from the FINALLY clause to pop up the return address from the stack and stored in a local variable.
Import java.net. *;
Import java.io. *;
Class withoutfinally
{
Public void foo () THROWS IOEXCEPTION
{
/ / Create a socket on any free port
Serversocket SS = New ServerSocket (0);
Try {
Socket Socket = ss.accept ();
// 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 accomplish this task, we call Close at / / 2, which is the last statement of this method. But what if an exception occurs in the TRY block? In this case, the Close call at // 2 will never happen. Therefore, you must capture this exception and insert another call to Close before // before re-issue this exception. This ensures that the socket is turned off before exiting the method.
This writes the code is cumbersome and easy to errors, but this is essential in situations without finaLY. Unfortunately, in languages without a finally mechanism, programmers may forget to organize their code in this way, resulting in resource vulnerabilities. The Finally clause in Java solved this problem. With Finally, the previous code can be rewritten as the following form:
Import java.net. *;
Import java.io. *;
Class withfinally
{
Public void foo2 () THROWS IOEXCEPTION
{
/ / Create a socket on any free port
Serversocket SS = New ServerSocket (0);
Try {
Socket Socket = ss.accept ();
// other code here ...
}
Finally {
ss.close ();
}
}
}
The Finally block ensures that the Close method is always performed, regardless of whether there is an exception in the TRY block. Therefore, it is ensured that the Close method will always be called before exiting the method. This way you can confirm that the socket is turned off and you have no resources. There is no need to have a Catch block in this method. In the first example, the CATCH block is only for closing the socket, now this is closed through Finally. If you really provide a Catch block, the code in the Finally block is executed after the CATCH block is completed. Finally blocks must be used in conjunction with TRY or TRY / CATCH blocks. In addition, it is impossible to exit the TRY block without performing its Finally block. If the finally block exists, it will always be executed. (Nothing, this statement is correct. There is a way to exit the TRY block without performing a Finally block. If the code performs a system.exit (0); statement, the application is endless without Execute Finally execution. On the other hand, if you dial off the power during the trory execution, Finally does not execute.)