Java Foundation--try-catch-finally

Source: Internet
Author: User
Tags throw exception truncated

Excerpt from other people's blogs/catch-up processing work
3 public static Boolean Catchmethod (){
4 System.out.print ("Call Catchmethod and return--->>");
5 return false;
6}
7//finally post-processing work
8 public static void Finallymethod () {
9 System.out.println ();
Ten System.out.print ("Call Finallymethod and do something--->>");
11}
A



1. Throw Exception, no finally, when catch meets return

1
2public static Boolean catchtest (){
3 Try{
4 int i = 10/0; Throw Exception, subsequent processing is denied
5 System.out.println ("I vaule is:" + i);
6 return true; Exception has been thrown, without getting the chance of being executed
7} catch (Exception e){
8 System.out.println ("--Exception--");
9 return Catchmethod (); Exception thrown, obtaining the opportunity to call the method and return the method value
10}
11}
A


Back-end output:

1
2--Exception--
3call Catchmethod and return--->> false
4


2. Throw Exception when a block of code with return,finally body in the catch body is executed before the catch executes a return

1
2public static Boolean CatchFinallyTest1 (){
3 Try{
4 int i = 10/0; Throw Exception, subsequent processing is denied
5 System.out.println ("I vaule is:" + i);
6 return true; Exception has been thrown, without getting the chance of being executed
7} catch (Exception e){
8 System.out.println ("--Exception--");
9 return Catchmethod (); The Exception throws, obtaining the opportunity to invoke the method, but the method value is returned after finally execution
Ten}finally{
Finallymethod (); Exception thrown, the finally code block is executed before the catch executes a return
12}
13}
-


Back-end output:

1
2--Exception--
3call Catchmethod and return--->>
4call Finallymethod and do something--->> false
5


3. Do not throw Exception, when the finally code block inside meet return,finally after execution will end the entire method

1
2public static Boolean CatchFinallyTest2 (){
3 Try{
4 int i = 10/2; Do not throw Exception
5 System.out.println ("I vaule is:" + i);
6 return true; // The opportunity to be executed, but execution needs to be executed after finally execution is completed
7} catch (Exception e){
8 System.out.println ("--Exception--");
9 return Catchmethod ();
Ten}finally{
Finallymethod ();
return false; Finally contains the return statement, this return will end this method, will not be executed after execution and then jump back to try or catch continue execution, the method ends, return false
13}
14}
the


Back-end output:

1
2i Vaule Is:5
3
4call Finallymethod and do something--->> false
5


4. Do not throw Exception, when the finally code block inside the System.exit () method will end and terminate the entire program, not just the method

1
2public static Boolean finallyexittest (){
3 Try{
4 int i = 10/2; Do not throw Exception
5 System.out.println ("I vaule is:" + i);
6 return true; Get the opportunity to be executed, but because finally has terminated the program, the return value has no chance of being returned
7} catch (Exception e){
8 System.out.println ("--Exception--");
9 return true;
Ten}finally {
Finallymethod ();
System.exit (0);//finally contains the System.exit () statement, System.exit () will exit the entire program, the program will be terminated
13}
14}
the


Back-end output:

1
2i Vaule Is:5
3
4call Finallymethod and do something--->>
5


5. Throw Exception, when catch and finally meet Return,catch return value will not be returned, finally return statement will end the entire method and return

1
2public static Boolean FinallyTest1 (){
3 Try{
4 int i = 10/0; Throw Exception, subsequent processing is denied
5 System.out.println ("I vaule is:" + i);
6 return true; Exception has been thrown, without getting the chance of being executed
7} catch (Exception e){
8 System.out.println ("--Exception--");
9 return true; Exception has been thrown to get the chance of being executed, but the return operation will be finally truncated
Ten}finally {
Finallymethod ();
return false; Return will end the entire method, returning false
13}
14}
the


Back-end output:

1
2--Exception--
3
4call Finallymethod and do something--->> false
5


6. Do not throw Exception, when finally the return value of Return,try will not be returned, finally the return statement will end the entire method and return

1
2public static Boolean FinallyTest2 (){
3 Try{
4 int i = 10/2; Do not throw Exception
5 System.out.println ("I vaule is:" + i);
6 return true; Gets the opportunity to be executed, but the return is truncated by finally
7} catch (Exception e){
8 System.out.println ("--Exception--");
9 return true;
Ten}finally {
Finallymethod ();
return false; Return ends this method, does not jump back to try or catch after execution, returns false
13}
14}
the


Back-end output:

1
2i Vaule Is:5
3
4call Finallymethod and do something--->> false
5

Java Foundation--try-catch-finally

Related Article

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.