java异常处理中的细节

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lslk9898/article/details/68061358

首先看一段代码

public class Test{
	public static String output="";
	public static void foo(int i){
		try {
			if(i==1){
				throw new Exception();
			}
			output +="1";
		} catch(Exception e){
			output+="2";
			return;
		} finally{
			output+="3";
		}
		output+="4";
	}
	public static void main(String args[]){
		foo(0);
		foo(1);
		System.out.println(output); 
	}
}
 
 

// 输出结果:13423
// 如果被调用的方法中用throw来抛出一个异常对象,但是该方法并没有处理该异常,
// 则在该方法中执行完finally语句块后,不会再执行finally之后的语句,而直接返回到
// 方法调用处,将异常交由调用该方法的方法处理,如果不处理将会checked exception;
// 如果被调用的方法中抛出的异常对象被catch处理了,则finally之后/的语句可以正常执行

 

猜你喜欢

转载自blog.csdn.net/lslk9898/article/details/68061358