The situation 1:try block does not throw exceptions in the try and finally blocks there are return statements [Java] view plain copy public static int noexception () {int i=10; try{System.out.println ("I in Try blocks is" +i); Return----I. }catch (Exception e) {I.; System.out.println ("I in Catch-form try blocks is" +i); Return----I. }finally{System.out.println ("I in finally-from try or catch blocks is" +i); Return----I. } }
Execution results:
I in try block Is10
I in Finally-from try or catch block Is9
The method value Is8
Order of execution: Executes the try block, executes the return statement, and then, without returning 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. It's not going to go back here. Returns the computed value in a try block
The situation 2:try block does not throw an exception, only the try has a return statement
Code: [Java] View plain copy 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 results:
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 the statement that executes the return in a try, does not go back, executes the finally block, returns to the try block, returns the last value of I in the try block after the execution of finally block is finished
Situation 3:try The exception try,catch,finally in the block there are return statements
Code: [Java] View plain copy 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";