您的位置:首页 > 编程语言 > Java开发

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

2014-02-12 15:05 781 查看
Java异常处理综合例子(try、catch、finally、throws、throw)

[java]
view plaincopyprint?

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);  
  
    }  
  
}  

博文转载至: http://blog.csdn.net/microtong
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐