Home  >  Article  >  Java  >  What are the characteristics and functions of the finally keyword in java

What are the characteristics and functions of the finally keyword in java

王林
王林forward
2020-09-01 16:00:142966browse

What are the characteristics and functions of the finally keyword in java

What are the characteristics of the finally keyword?

The statement body controlled by finally will definitely be executed.

(Video tutorial recommendation: java course)

Special case: jvm exited before execution to finally (such as System.exit(0)).

What is the function of finally keyword?

The function of finally is to release resources, which can be seen in IO stream operations and database operations.

(Related tutorial recommendations: Getting started with java)

Example:

package com.heima.exception;

public class Demo07_Finally {

	public static void main(String[] args) {
		try {
			System.out.println(10/0);
		} catch (Exception e) {
			System.out.println("除数为零了");
			System.exit(0);								//退出jvm虚拟机
			// return语句相当于是方法的最后一口气,那么在他将死之前会看一看有没有
			// finally帮其完成遗愿,如果有就将finally执行后在彻底返回。
			return;
		} finally {
			System.out.println("看看我执行了吗");
		}
	}
}

The above is the detailed content of What are the characteristics and functions of the finally keyword in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete