Java异常处理综合例子(try、catch、finally、throws、throw)

Java异常处理综合例子(try、catch、finally、throws、throw)

佟强 2009年11月4日 http://blog.csdn.net/microtong

package cn.edu.uibe.exp; class MyExp extends Exception{ private static final long serialVersionUID = 1L; MyExp(){ super("自定义异常"); } } public class Exp2 { public void f1(int n) { System.out.println("f1前面"); f2(n); System.out.println("f1后面"); } public void f2(int n) { System.out.println("f2前面"); try{ System.out.println("try的前面"); f3(n); System.out.println("try的后面"); //发生异常时不会输出 }catch(MyExp e){ System.err.println(e.getMessage()); }finally{ System.out.println("无论是否发生异常"); } System.out.println("f2后面"); } public void f3(int n) throws MyExp{ System.out.println("f3前面"); f4(n); System.out.println("f3后面"); //发生异常时不会输出 } public void f4(int n) throws MyExp{ System.out.println("f4前面"); if(n<0){ throw new MyExp(); } System.out.println(n); //发生异常时不会输出 System.out.println("f4后面"); //发生异常时不会输出 } public static void main(String[] args) { Exp2 exp = new Exp2(); exp.f1(-4); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值