12.8.3 缺憾:异常丢失

来源:互联网 发布:香港进出口数据 编辑:程序博客网 时间:2024/06/09 21:56
使用finally可能会导致异常丢失, 具体可能有2种情况 : 
1 在finally语句中抛出其它异常, 取代原有异常. 

2 在finally语句中使用return返回, 原有异常不再抛出.

package com.cnsuning.src;public class Main {public Main() {// TODO Auto-generated constructor stub}public static void main(String[] args) {Main m = new Main();try {m.test(0, 0);m.test(1, 2);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void test(int a, int b) throws Exception{try{if(a == b && a == 0){throw new Exception("两个数不能都为零");}System.out.println("a:"+a);System.out.println("b:"+b);return;}finally{System.out.println("finally statement");return;}}}


0 0
原创粉丝点击