public static String Test0 () {
String A;
int b=0;
try{
b = 8/0; (1)------0 cannot be divisor, this line is abnormal, the return in try does not execute
return "Try"; ========================= did not perform ======================================
}catch (Exception e) {
E.printstacktrace ()//(2)//At this time the program will not terminate, and will continue to execute
}
A = string.valueof (b); (3)
return a+b; (4)
}
Return is available in try and catch
public static String Test0_1 () {
String A;
int b;
try{
b = 8/0; (1) 0 cannot be divisor, this line is abnormal, the return in try does not execute
A = string.valueof (b); =================== not executed
return a+b; =================== not executed
}catch (Exception e) {
E.printstacktrace (); (2)
Return "test0_1 a mistake"; (3)
}
String s= "Zhaohao"; There are return in try and catch, and anything here will give you an error--------no add, no error.
System.out.println ("Haha, Zhaohao"); There are return in try and catch, and anything here will give you an error--------no add, no error.
return a+b; Try and catch all have return, here add return will be an error--------don't let add, error
}
public static String Test0_2 () {
String A;
int b=0;
try{
b = 8/0;
}catch (Exception e) {
b=100;
}
A = string.valueof (b); Will execute here ===================== b is 100
return A; Will execute here =====================
}
//=============================================================
========== Summary: The return in try will execute after finnally execution ================
//=============================================================
public static int test1 () {
int m = 3;
int n = 2;
int a = 7;
int B = 8;
int s = 0;
String str= "";
try{
S=m+n; (1)
Str= "I am a string in a try";
return s; (2) First execute this = = = (the value of S has been confirmed, finally will not modify it), = = The second execution finally =================== (5) ended, this time will really return the value
catch (Exception e) {
finally {
=========== the changes to int and string in try are not valid =================================?????????????????????????????
S=a+b; (3)
Str= "I am the string in finally";
SYSTEM.OUT.PRINTLN ("Do finally Haha"); (4)
}
return s; ============== will not perform =========== .................... ..... ...
}
//=============================================================
========== Summary: The return in try will execute after finnally execution ================
//=============================================================
Execution results:
Do finally haha
5
public static String Test1_1 () {
String a = "in Try";
try{
return A; Because the return is in the finnally, the return does not execute
catch (Exception e) {
finally {//from the eclpise alarm can be seen, finally inside do not recommend a return statement
A = "The return value in finally";
SYSTEM.OUT.PRINTLN ("Do Finally in Test1_1");
return A; Commenting out this, eclipse will no longer warn ================================
}
}
return Result:
Do finally in the Test1_1
The return value in finally
/**
Summary
* Return statement, finally there is no suggestion to put the return statement, as needed, can be placed in the try and catch inside
*
*/
public static int test2 () {
int a = 1;
try{
return A;
catch (Exception e) {
finally {
A = 2;
SYSTEM.OUT.PRINTLN ("do finally");
}
return A;
//Obviously, finally, the change is invalid and returns the A=1
/**
Summary
* Return statement, finally there is no suggestion to put the return statement, as needed, can be placed in the try and catch inside
*
*/
public static int Test2_1 () {
int a = 1;
try{
return A;
catch (Exception e) {
finally {
A = 222222222;
SYSTEM.OUT.PRINTLN ("Do finally Test2_1");
return A;
}
//Obviously, a takes the value of Finally, a=2222222222
Helper class, converts integers to strings
Static Class Helper {
int A;
Public String toString () {
Return string.valueof (a);
}
}
public static Helper Test3 () {
Helper h = new Helper ();
H.A = 1;
try{
return h;
catch (Exception e) {
finally {
H.A = 2; Changes to H.A are working!!
Because a handle is returned in a try, the contents of the object that it points to can be changed
SYSTEM.OUT.PRINTLN ("do finally");
}
return h; This is not going to be executed.
}
public static Helper Test3_1 () {
Helper h = new Helper ();
H.A = 1;
try{
return h;
catch (Exception e) {
finally {
H.A = 2; Back to a=2, this is needless to say.
SYSTEM.OUT.PRINTLN ("do finally");
return h;
}
}
/**
Summary
* Return statement, finally there is no suggestion to put the return statement, as needed, can be placed in the try and catch inside
*
*/
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.