Try Catch finally return run order

Source: Internet
Author: User
Tags finally block try catch

First, let's understand two sets of concepts: Try Catch finally and return

1.try Catch finally

First say try Catch,

(1) A try statement used to enclose a snippet of code that might have an exception. Try is the statement that finds the problem, and then jumps into catch{} when it finds the exception, as follows:

try{

Code snippets that may appear to be abnormal

}

(2) A catch statement, which is used to catch an exception that occurs in a try statement, and resolves A. Catch statement block that can occur more than once.

catch (Exception_type e) {

Code snippet to solve the problem

}

(3) Finally block, finally appears at the end of the try statement, the statement in the finally block is bound to execute, regardless of whether there is an exception in the try.
finally{

Code Snippets
}

2.return

Two functions:

(1) The general is used in the method with the inverse value, used to return the value of the method specified type, and the end of the method execution;

(2) can also be used in the return value of void method, used to terminate the operation of the method;

3. For a basic understanding of the above is not afraid to understand the try Catch finally return run order, the following code as an example:

public static void Main (string[] args) {
String str = NULL;
System.out.println (Demo (str));
}

(1) when no finally:

(1.1)

public static string demo (String str) {
try {
System.out.println (Str.charat (1)); Construct exception
return "1";

} catch (Exception e) {
Return "2";
}

}

Run Result: 2 (no exception returned 1, program end; When an exception occurs, return 2, end of program)

(1.2)

public static string demo (String str) {
try {
System.out.println (Str.charat (1));
} catch (Exception e) {
Return "2";

}
Return "4";

}

Run Result: 2 (no exception returned 4, program end; When an exception occurs, return 2, end of program)

(1.3)

public static string demo (String str) {
try {
System.out.println (Str.charat (1));
return "1";
} catch (Exception e) {
}
Return "4";
}

Running Result: 4 (no exception returned 1, program end; When an exception occurs, the program jumps to the catch and does nothing, then runs down, returns "4", ends the program)

(1.4)

public static string demo (String str) {
try {
return "1";
} catch (Exception e) {
}
Return "4";
}

Run Result: 1 (program runs from top to bottom, return "1", End of program)

Other circumstances no longer one by one repeat ...

(2) When there is a finally:

(2.1)

public static string demo (String str) {

try {
System.out.println (Str.charat (1));
return "1";
} catch (Exception e) {
Return "2";
}
finally{
Return "3";
}
}

(2.2)

public static string demo (String str) {
try {
return "1";
} catch (Exception e) {
Return "2";
}
finally{
Return "3";
}
}

Note: When there is a finally, a single finally has a return statement, then no matter what the previous return (if there is an exception), is overwritten by the value inside the finally (and finally, once there is return, immediately return, the program ends, No longer runs the following program), as the previous two examples all return 3: Just (2.1) is first 2 after 3, (2.2) is 1 after 3

(3) Other conditions:

(3.1)

public static string demo (String str) {
try {
System.out.println (Str.charat (1)); Abnormal place
return "1";
} catch (Exception e) {
try {
throw new Exception (e);
} catch (Exception E1) {
E1.printstacktrace ();
}
Return "2";
}
}

Run Result: 2, this is because the program has an exception at the exception, jumps directly into the outer catch{}, and in the catch despite a try catch, but the program in catch{} is top-down can be run to return "2", place, return, program end

(3.2)

public static string demo (String str) {
try {
System.out.println (Str.charat (1));
return "1";
} catch (Exception e) {
try {
throw new Exception (e);
} catch (Exception E1) {
E1.printstacktrace ();
}
Return "2";
}
finally{
Return "3";
}
}

Run Result: 3, this run process is the same as the previous example, know that after return "2", will jump out of the outer catch{}, run finally{}, encountered in this return "3", return, the end of the program

The above is my simple summary, if there are errors, welcome criticism, please specify the author and source, thank you

Try Catch finally return run order

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.