Try and finally in Java

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.