异常执行结果随笔

前段时间有朋友问我异常执行顺序问题,这里简单记录下哈。
伪代码描述,当j=0和j=1,输出结果分别是什么?

int i = 0;
int j = 01;
try {
  j = i / j;
	System.out.println(i);
	return i++;
} catch (Exception e) {
	System.out.println(i);
	return ++i;
} finally {
	System.out.println(i);
}

talk is cheap, show me the code!
当j=0时候,输出结果为

0
1

异常执行结果随笔_第1张图片
当j=1时候,输出结果为

0
1

异常执行结果随笔_第2张图片
总结:当j=0或j=1,结果都是一样,j=0先走catch再走finally。
j=1,不走catch,最后走finally。

0
1

你可能感兴趣的:(算法,java,开发语言)