package cn.com.exception; import java.io.FileNotFoundException; import java.io.IOException; /** * 把所有异常全部抛出...不做处理... 这样既防止吞掉异常(try..catch)又不用去声明抛出异常... * 把所有异常抛给上层调用者去处理. * @author GuoBaoqiang * 2014年7月24日 */ public class Test { void throwRuntimeException(int type) { try { switch (type) { case 0: throw new FileNotFoundException(); case 1: throw new IOException(); case 2: throw new RuntimeException("where am i "); default: return; } } catch (Exception e) { throw new RuntimeException(e); } } public void pack_exception() { try { for (int i = 0; i < 4; i++) { throwRuntimeException(i); } // ..... } catch (Exception re) { try { throw re.getCause(); } catch (FileNotFoundException e) { System.out.println("FileNotFoundException:" + e); } catch (IOException e) { System.out.println("IOException:" + e); } catch (Throwable e) { System.out.println("Throwable:" + e); } } } }


asddwh LV13
2023年12月25日
1443251642 LV1
2022年12月19日
yyds123456 LV2
2022年12月2日
wei12356 LV1
2022年1月14日
开发哈哈
2022年1月11日
暂无贡献等级
李晓珂 LV2
2021年12月14日
Dimoo1
2021年11月29日
暂无贡献等级
tangjj7260 LV18
2021年11月12日
Trickster LV9
2021年10月19日
329512801 LV28
2021年7月8日