Java异常捕获机制中的finally

版权声明:转载注明来源。Keep Learning and Coding. https://blog.csdn.net/a771581211/article/details/88647694
package day08;
/**
 * finally块
 * finally块定义在异常捕获机制的最后
 * 可以直接跟在try块之后或最后一个catch块之后。
 * finally块中的代码一定会执行,无论try块中的代码是否抛出异常。
 * 所以通常会把释放资源等操作放在finally中,例如关闭流等。
 * @author kaixu
 *
 */
public class ExceptionDemo2 {

	public static void main(String[] args) {
		System.out.println("程序开始");
		try{
			String str = null;
			str = " ";
			System.out.println(str.length());
		}catch(Exception e){
			System.err.println("程序出错");
		}finally{
			System.out.println("finally中的代码执行了。");
		}
		System.out.println("程序结束");

	}

}

猜你喜欢

转载自blog.csdn.net/a771581211/article/details/88647694