View code
1 Package Exercise;
2 /**
3 * Try, finally
4 * @ Author Administrator
5 *
6 */
7 Public Class Testreturnandfinally {
8 Public Static Void Main (string [] ARGs ){
9 // Todo auto-generated method stub
10 System. Out. println ( New Testreturnandfinally (). Test ());;
11 }
12
13 Static Int Test (){
14 Int X = 1;
15 Try {
16 Return X;
17 }
18 Finally {
19 ++ X;
20 }
21 }
22 }
Result: 1
View code
1 Package Exercise;
2 /**
3 * Try, finally
4 * @ Author Administrator
5 *
6 */
7 Public Class Testreturnandfinally3 {
8 Public Static Void Main (string [] ARGs ){
9 // Todo auto-generated method stub
10 System. Out. println ( New Testreturnandfinally3 (). Test ());;
11 }
12
13 Static Stringbuffer test (){
14 Stringbuffer A = New Stringbuffer ("init ");
15
16 Try {
17 A. append ("try ");
18 Return A;
19 }
20 Finally {
21 A. append ("finally ");
22 }
23 }
24 }
Result: init try finally
1 Package Exercise;
2 /**
3 * Try, finally
4 * @ Author Administrator
5 *
6 */
7 Public Class Testreturnandfinally3 {
8 Public Static Void Main (string [] ARGs ){
9 // Todo auto-generated method stub
10 System. Out. println ( New Testreturnandfinally3 (). Test ());;
11 }
12
13 Static String test (){
14 String A = New String ("init ");
15
16 Try {
17 A = New String ("try ");
18 Return A;
19 }
20 Finally {
21 A = New String ("finally ");
22 }
23 }
24 Result: T
View code
1 Package Exercise;
2
3 /**
4 * Common class
5 * @ Author Administrator
6 *
7 */
8 Public Class Quote {
9 Public Int A = 1;
10 }
11
12
13
14
15 Package Exercise;
16 /**
17 * Try, finally
18 * @ Author Administrator
19 *
20 */
21 Public Class Testreturnandfinally2 {
22 Public Static Void Main (string [] ARGs ){
23 // Todo auto-generated method stub
24 System. Out. println ( New Testreturnandfinally2 (). Test (). );;
25 }
26
27 Static Quote test (){
28 Quote q = New Quote ();
29
30 Try {
31 Return Q;
32 }
33 Finally {
34 Q. A ++;
35 }
36 }
37 }
Result: 2
Conclusion: any call to the Return Statement in try or catch will first execute the finally statement, provided that the finally statement exists. If a return statement exists in finallyProgramSo the return in finally will be returned. The Compiler implements the return in finally as a warning.