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 is the execution order of try-catch-finally grammar blocks in Java?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the execution order of try-catch-finally grammar blocks in Java". The explanation in this article is simple and clear and easy to learn and understand. please follow the editor's ideas to study and learn "what is the execution order of try-catch-finally grammar blocks in Java".

I. try-catch sentence block

We can take a look at the following procedure:

Public static void main (String [] args) {

System.out.println (handleException0 ())

}

/ * *

* all try,catch 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.

1. 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 "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 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 ())

}

/ * *

* return is available in both 3:try and finally

* @ 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 return value can be returned

}

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 return is available in all cases.

* @ 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 execution 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: 10at

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.

Thank you for your reading. the above is the content of "what is the execution order of try-catch-finally grammar blocks in Java". After the study of this article, I believe you have a deeper understanding of the execution order of try-catch-finally grammar blocks in Java, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 0

*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