Home > Java > javaTutorial > body text

Detailed explanation of examples of finally statement in java

零下一度
Release: 2017-07-20 19:02:50
Original
1718 people have browsed it

There are at least two situations in which the finally statement will not be executed:
(1) The try statement is not executed. If return is returned before the try statement, the finally statement will not be executed. This also shows that the necessary but not sufficient condition for the finally statement to be executed is: the corresponding try statement must be executed.

(2) There is a statement like System.exit(0); in the try block|catch block. System.exit(0) terminates the Java virtual machine JVM. Even the JVM is stopped and everything is over. Of course, the finally statement will not be executed.

In try-catch-finally, when return encounters finally, return is invalid for finally, that is:

1. When return is made in try catch block, finally will also be executed.

2. The return statement in finally will overwrite the effect of the return statement in the try catch block.

Conclusion: The return statement is not necessarily the exit of the function. When the return is executed, the value after the return is just copied to the return value variable.


class Exc{
	int a;
	int b;
}


public class Except {
	@SuppressWarnings("finally")
	static int   compute (){
		Exc e = new Exc();
		e.a = 10;
		e.b = 10;
		int res = 0 ;
		try{
			res = e.a / e.b;
			System.out.println("try ……");
			return res + 1;
			
		}catch(NullPointerException e1){
			System.out.println("NullPointerException occured");
		}catch(ArithmeticException  e1){
			System.out.println("ArithmeticException occured");
		}catch(Exception e3){
			System.out.println("Exception occured");
		}finally{
			System.out.println("finnaly occured");
		}
		System.out.println(res);
		
		return res+3;
	}
	
	public static void main(String[] args){
		int b =  compute();
		System.out.println("mian b= "+b);
	}
}
Copy after login

Output:

try ……
finnaly occured
mian b= 2
Copy after login

Conclusion: If there is no exception, execute the code block in try until the return in try, then execute the code block in finally. After finally is executed, return to try Execute return. Exit function.

class Exc{
	int a;
	int b;
}


public class Except {
	@SuppressWarnings("finally")
	static int   compute (){
		Exc e = new Exc();
//		e.a = 10;
//		e.b = 10;
		int res = 0 ;
		try{
			res = e.a / e.b;
			System.out.println("try ……");
			return res + 1;
			
		}catch(NullPointerException e1){
			System.out.println("NullPointerException occured");
		}catch(ArithmeticException  e1){
			System.out.println("ArithmeticException occured");
		}catch(Exception e3){
			System.out.println("Exception occured");
		}finally{
			System.out.println("finnaly occured");
		}
		System.out.println(res);
		
		return res+3;
	}
	
	public static void main(String[] args){
		int b =  compute();
		System.out.println("mian b= "+b);
	}
}
Copy after login

Output:

ArithmeticException occured
finnaly occured
0
mian b= 3
Copy after login

Conclusion: If there is an exception in try, jump to the exception code block captured by catch at the exception statement. After executing catch, execute finally and jump out of try{}catch{} finally{}, continue to execute downwards, and the subsequent statements in try will not be executed.

The above is the detailed content of Detailed explanation of examples of finally statement in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!