The execution order of try-catch-finally-return in Java exception capture

Case 1: No exception is thrown in the try block, both try and finally blocks have return statements

 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;
  }
 }


Results of the:

i in try block is10
i in finally - from try or catch block is9
the method value is8

Execution sequence: execute the try block, when the return statement is executed, execute the return statement first, --i, but do not return to the main method, execute the finally block, encounter the return statement in the finally block, execute --i, and put The value is returned to the main method, and the value calculated in the try block will not be returned here.

Case 2: There is no exception thrown in the try block, only the return statement in the try

Code:

	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;
		}
	}


Results of the:

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

Sequence: After executing the return statement in try, do not return, execute the finally block, after the finally block is executed, return to the try block, and return the last value of i in the try block

Case 3: An exception is thrown in the try block. There are return statements in try, catch, and finally

Code:

	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;
		}
	}


Results of the:

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

Sequence, after throwing an exception, execute the catch block. After the --i of the return of the catch block is executed, it does not return directly but executes finally, because there is a return statement in finally, so execute, return result 6

In case 4, there is return in catch, but not in finally. Same as above. 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.

Case 5: There are exceptions in try and catch, and there is no return statement in finally

	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;
		}
	}


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
Exception in thread "main" java.lang.ArithmeticException: / by zero
at exception.ExceptionTest0123.CatchException(ExceptionTest0123.java:29)
at exception.ExceptionTest0123.main(ExceptionTest0123.java:17)

Execution sequence: an exception occurs in the try block, goes to the catch, executes to the exception, and executes to the finally.

Case 6: There are exceptions in try and catch, and there is a return in finally

	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: an exception occurs in the try block to catch, an exception occurs in catch to finally, and finally is executed to the return statement, without checking for exceptions

There is no catch, only try and finally, the execution order is similar to the above cases, but the execution of the catch block is missing

Welcome everyone to add corrections

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324142824&siteId=291194637