In Java, the relationship between finally and return is resolved, finallyreturn

Source: Internet
Author: User

In Java, the relationship between finally and return is resolved, finallyreturn

This article focuses on the relationship between finally and return in Java. The specific introduction and examples are as follows.

Summary of the finally and return relationships

1. the try block contains System. exit (0) is a statement that terminates the JVM of the Java Virtual Machine (JVM), stops the JVM, and ends all the statements. Of course, the finally statement will not be executed.
2. In other cases, finally statements will inevitably be executed. Therefore, you can release some resources here.

(1) The return in finally will overwrite the limit urn In try/catch.
(2) There is a warning when writing the return statement in finally, because it will prevent the function from throwing an exception and return it normally.

Package com. demo. test; public class FinallyAndReturn {private static void finallyAndTryReturn () {try {System. out. println ("finallyAndTryReturn-> try"); return;} catch (Exception e) {System. out. println ("finallyAndTryReturn-> catch");} finally {System. out. println ("finallyAndTryReturn-> finally") ;}} private static void finallyAndCatchReturn () {try {System. out. println ("finallyAndCatchReturn-> try"); throw New Exception ();} catch (Exception e) {System. out. println ("finallyAndCatchReturn-> catch"); return;} finally {System. out. println ("finallyAndCatchReturn-> finally") ;}// the finally statement is executed after the return statement of try is executed and before the return statement is returned. Private static String tryReturn () {String str = "initialized"; try {System. out. println ("tryReturn-> try"); str = "try"; return str;} catch (Exception e) {System. out. println ("tryReturn-> catch"); str = "catch";} finally {System. out. println ("tryReturn-> finally"); str = "finally";} return null;} private static String tryReturnAndFinallyReturn () {String str = "initialized"; try {System. out. println ("tryReturn AndFinallyReturn-> try "); str =" try "; return str;} catch (Exception e) {System. out. println ("tryReturnAndFinallyReturn-> catch"); str = "catch";} finally {System. out. println ("tryReturnAndFinallyReturn-> finally");/** Warning: finally block does not complete normally * If the finally block contains the return statement, even if the previous catch block re-throws an exception, the statement that calls this method will not get the catch Block re-throw exception, but will get the finally block return value and will not catch the exception. */Str = "finally"; return str ;}} private static String tryThrowAndFinallyReturn () throws Exception {String str = "initialized"; try {System. out. println ("tryThrowAndFinallyReturn-> try"); str = "try"; throw new Exception ();} catch (Exception e) {System. out. println ("tryThrowAndFinallyReturn-> catch"); str = "catch"; throw new Exception ();} finally {System. out. println ("tryThrowAndFinallyReturn-> finally "); /** Warning: finally block does not complete normally * If the finally block contains the return statement, even if the previous catch block throws an exception again, then, the statement that calls this method does not obtain the exception thrown by the catch Block, but returns the finally block and does not capture the exception. */Str = "finally"; return str ;}} private static void finallyAndRuntimeException () {try {System. out. println ("finallyAndRuntimeException-> try"); throw new RuntimeException ();} catch (Exception e) {System. out. println ("finallyAndRuntimeException-> catch");} finally {System. out. println ("finallyAndRuntimeException-> finally") ;}} private static void finallyAndExit () {try {System. out. println ("finallyAndExit -> Try "); // System. exit (0); terminate the JVM of the Java Virtual Machine (JVM). Even the JVM stops and all ends. Of course, the finally statement will not be executed. System. exit (0);} catch (Exception e) {System. out. println ("finallyAndExit-> catch");} finally {System. out. println ("finallyAndExit-> finally") ;}} public static void main (String [] args) {finallyAndTryReturn (); System. out. println (); finallyAndCatchReturn (); System. out. println (); System. out. println ("tryReturn return->" + tryReturn (); System. out. println (); System. out. println ("tryReturnAndFinallyReturn return->" + tryReturnAndFinallyReturn (); System. out. println (); try {System. out. println ("tryThrowAndFinallyReturn return->" + tryThrowAndFinallyReturn ();} catch (Exception e) {e. printStackTrace ();} System. out. println (); finallyAndRuntimeException (); System. out. println (); finallyAndExit ();}}

Demo result:

Summary

The above is all the content about the link instance parsing of finally and return in Java, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.