Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the situations of return in the try, catch and finally statements of Java?

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are all kinds of situations of return in Java's try, catch and finally sentences". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java's try, catch, finally sentences in the various situations of return is what" it!

I. try-catch sentence block

We can take a look at the following procedure:

Public static void main (String [] args) {System.out.println (handleException0 ());} / * try,catch all have return * @ return * / private static String handleException0 () {try {System.out.println ("try start"); String s = null; int length = s.charAt (0); System.out.println ("try end"); return "return value of try block" } catch (Exception e) {System.out.println ("exception caught"); return "return value of catch";}}

Execution result:

Try began to capture the return value of the exception catch

Analysis: the program first executes the code inside the try block, and there are exceptions found in the try block, and the code behind the try block will not be executed (naturally, it will not be return), then enter the catch block that matches the exception, and then enter the catch block to complete the code execution. When the return statement inside the catch is executed, the program is aborted, and then the final result of this return is returned.

II. Try-catch-finally sentence blocks

I have divided this grammar block into four situations, which are listed below.

In the first case, there is a return in the try block and an exception is caught

Example 1:

Public static void main (String [] args) {String result = handleException1 (); System.out.println (result);} private static String handleException1 () {try {System.out.println ("try start"); String str = null; int length = str.length (); System.out.println ("try end");} catch (Exception e) {System.out.println ("exception caught") } finally {System.out.println ("finally block execution completed");} return "final result";}

The result of example 1 is as follows:

Try begins to catch the exception finally block, and the final result is finished.

Example 2:

Public static void main (String [] args) {String result = handleException2 (); System.out.println (result);} private static String handleException2 () {try {System.out.println ("try start"); String str = null; int length = str.length (); System.out.println ("try end"); return "try block return value";} catch (Exception e) {System.out.println ("exception caught") } finally {System.out.println ("finally block execution completed");} return "final result";}

The execution result of example 2 is as follows:

Try begins to catch the exception finally block, and the final result is finished.

Analysis: first of all, the results of examples 1 and 2 are obvious. When an exception is encountered, go directly to the corresponding catch block, then continue to execute the finallly statement block, and finally return the return result.

The second case: there is a return in the try block, but no exception is caught

Example 3:

Consider: the following code try statement block has a return statement, so whether to execute the try statement block directly return exit method?

Public static void main (String [] args) {String result = handleException3 (); System.out.println (result);} private static String handleException3 () {try {System.out.println ("); return" return value of try block ";} catch (Exception e) {System.out.println (" exception caught ");} finally {System.out.println (" finally block execution completed ");} return" final result " }

The execution result of example 3 is as follows:

The return value of the finally block after the execution of the try block

Analysis: the result of example 3, in fact, we can look at the specific execution flow of the program through the break point. Through the break point, we can find that the code first executes the code in the try block. When the return statement is executed, the handleException3 method does not end immediately, but continues to execute the code in the finally block. After the code in the finally block is finished, it immediately goes back to the return statement of the try block and returns the final result. Execution of the handleException method is complete.

The third case: both the try block and the finally contain return.

Example 4:

Public static void main (String [] args) {System.out.println (handleException4 ());} / * case return * @ return * / private static String handleException4 () {try {System.out.println ("); return" return value of try block ";} catch (Exception e) {System.out.println (" exception caught ") } finally {System.out.println ("finally block execution completed"); return "return value of finally";} / / return "final result"; / / No more return values}

The implementation result of example 4:

The return value of finally after the execution of finally block is completed.

Analysis: it should be noted that when there is return in both the try block and the finally, the return keyword is no longer allowed outside the try/catch/finally grammar block. Let's look at the specific execution process of the code by breaking points in the program. The code first executes the code in the try block. When the return statement is executed, the handleException4 method does not end immediately, but continues to execute the code in the finally block. When it is found that there is a return in the finally block, it directly returns the return value (that is, the final result) in the finally, and the handleException4 method is executed.

The fourth case: try block, catch block, finally block all have return.

Example 5:

Public static void main (String [] args) {System.out.println (handleException5 ());} / * case 4 there are return * @ return * / private static String handleException5 () {try {System.out.println ("try start"); int [] array = {1,2,3}; int I = array [10]; System.out.println ("try end") Return "return value of try block";} catch (Exception e) {e.printStackTrace (); / / this line of code is actually the specific information of the printout exception System.out.println ("exception caught"); return "return value of catch";} finally {System.out.println ("finally block completed"); return "return value of finally" } / / return "final result";}

The implementation result of example 5:

Try begins to catch exception finally block and returns the value of finally after execution: java.lang.ArrayIndexOutOfBoundsException: 10 at com.example.javabasic.javabasic.ExceptionAndError.TryCatchFinally.handleException5 (TryCatchFinally.java:25) at com.example.javabasic.javabasic.ExceptionAndError.TryCatchFinally.main (TryCatchFinally.java:14)

Analysis: the program first executes the code in the try block, and an exception is found in the try block, and the code behind the try block will not be executed (naturally, it will not return), then enter the catch block that matches the exception, and then enter the catch block to finish executing the code. When the return statement in the catch is executed, the program will not immediately terminate, but will continue to execute the code of the finally block, and finally execute the return in the finally. Then return the final result of this return.

At this point, I believe that everyone has a deeper understanding of "what are the various situations of return in Java's try, catch, and finally sentences". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 294

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report