Java 基础(finally的使用)

ReturnExceptionDemo.java

package com.klvchen.java2;

public class ReturnExceptionDemo {
	static void methodA() {
		try {
			System.out.println("进入方法A");
			throw new RuntimeException("制造异常");
		} finally {
			System.out.println("用A方法的finally");
		} 
	}
	
	static void methodB() {
		try {
			System.out.println("进入方法B");
			return;
		} finally {
			System.out.println("调用B方法的finally");
		}
	}
	
	public static void main(String[] args) {
		try {
			methodA();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		methodB();
	}

}

ReturnExceptionDemo2.java

package com.klvchen.java2;

public class ReturnExceptionDemo2 {
	
	public static void main(String[] args) {
		
		int a = 1;
	
		try {
			a = 2;
			throw new RuntimeException("抛出异常");
		} catch (Exception e) {
			a = 3;
			System.out.println(e.getMessage());
			a = 4;
		}finally {
			a = 5;
		}

		System.out.println(a);
	}
	
}

原文地址:https://www.cnblogs.com/klvchen/p/14578702.html