There is a return statement in the try and finally block where no exception is thrown in the 1:try block
[Java]View PlainCopy
- Public static int noexception () {
- int i=10;
- try{
- System.out.println ("I in Try block is" +i);
- return-I.;
- }catch (Exception e) {
- I.;
- System.out.println ("I in Catch-form try block is" +i);
- return-I.;
- }finally{
- System.out.println ("I in Finally-from try or catch block is" +i);
- return-I.;
- }
- }
Execution Result:
I in try block Is10
I in Finally-from try or catch block Is9
The method value Is8
Execution order: Executes a try block, executes to the return statement, executes the return statement first, but does not return to the main method, executes a finally block, encounters a return statement in a finally block, executes-I, and returns the value to the Main method, We're not going back here. Returns the computed value in the try Block
The case does not throw an exception in the 2:try block, only the return statement in the try
Code:
[Java]View PlainCopy
- Public static int noexception () {
- int i=10;
- try{
- System.out.println ("I in try block is--" +i);
- return-I.;
- }catch (Exception e) {
- I.;
- System.out.println ("I in Catch-form try block is--" +i);
- return-I.;
- }finally{
- System.out.println ("I in Finally-from try or catch block is--" +i);
- I.;
- System.out.println ("I in finally block is--" +i);
- //return-I.;
- }
- }
Execution Result:
I in try block is--10
I in Finally-from try or catch block is--9
I in finally block is--8
The method value is--9
Order: After executing the return statement in try, do not return, execute finally block, after finally block execution is finished, return to the try block, return I in the try block the last value
Case 3:try The exception is thrown in the block Try,catch,finally has a return statement
Code:
[Java]View PlainCopy
- Public static int withexception () {
- int i=10;
- try{
- System.out.println ("I in try block is--" +i);
- i = i/0;
- return-I.;
- }catch (Exception e) {
- System.out.println ("I in Catch-form try block is--" +i);
- I.;
- System.out.println ("I in Catch block is--" +i);
- return-I.;
- }finally{
- System.out.println ("I in Finally-from try or catch block is--" +i);
- I.;
- System.out.println ("I in finally block is--" +i);
- return-I.;
- }
- }
Execution Result:
I in try block is--10
I in Catch-form try block is--10
I in Catch block is--9
I in Finally-from try or catch block is--8
I in finally block is--7
The method value is--6
Order, after throwing an exception, execute the CATCH block, after the return of the Catch block is finished, do not return directly but do finally, because finally there is a return statement, so, execute, return the result 6
There is no return,finally in the 4,catch, Ibid., after the finally statement is executed, the value after executing the return statement in the catch is still returned, not the value modified in finally
There are exceptions in both 5:try and catch, and no return statement in finally
[Java]View PlainCopy
- Public static int catchexception () {
- int i=10;
- try{
- System.out.println ("I in try block is--" +i);
- i=i/0;
- return-I.;
- }catch (Exception e) {
- System.out.println ("I in Catch-form try block is--" +i);
- int j = i/0;
- return-I.;
- }finally{
- System.out.println ("I in Finally-from try or catch block is--" +i);
- I.;
- System.out.println ("I in finally block is--" +i);
- //return-I.;
- }
- }
Results:
I in try block is--10
I in Catch-form try block is--10
I in Finally-from try or catch block is--10
I in finally block is--9
Exception in thread "main" java.lang.ArithmeticException:/By zero
At exception. Exceptiontest0123.catchexception (exceptiontest0123.java:29)
At exception. Exceptiontest0123.main (EXCEPTIONTEST0123.JAVA:17)
Execution order: Exception in try block, into catch, execute to exception, execute in Finally, Judge find exception after finally execution, throw
There are exceptions in the 6:try,catch, and in finally there is a return
[JavaScript]View PlainCopy
- Public static int catchexception () {
- int i=10;
- try{
- System.out.println ("I in try block is--" +i);
- i=i/0;
- return-I.;
- }catch (Exception e) {
- System.out.println ("I in Catch-form try block is--" +i);
- int j = i/0;
- return-I.;
- }finally{
- System.out.println ("I in Finally-from try or catch block is--" +i);
- I.;
- System.out.println ("I in finally block is--" +i);
- return-I.;
- }
- }
Operation Result:
I in try block is--10
I in Catch-form try block is--10
I in Finally-from try or catch block is--10
I in finally block is--9
The method value Is--8
Execution order: Exception occurred in try block to Catch,catch in exception to finally,finally in execution to return statement, not check exception
There are no catch, only try and finally, the execution order is similar to the above, except that the catch block is less executed.
Java exception capture Try-catch-finally-return execution order-Reprint