首页 > 学院 > 开发设计 > 正文

java中如何制定自定义异常

2019-11-14 23:29:11
字体:
来源:转载
供稿:网友
java中如何制定自定义异常 Posted on 2015-03-27 15:19 思思博士 阅读(...) 评论(...) 编辑 收藏
 1 package gys; 2  3 public class ExpetionTest { 4     public static void main(String[] args) { 5         NumberCheck n=new NumberCheck(); 6         try { 7             System.out.PRintln(n.division(1,-3)); 8         } catch (ZerpSubException e) {         9             System.out.println(e.getMessage());            10         } catch (ZeroException e) {        11             System.out.println(e.getMessage());            12         }13         catch(Exception ex){14             System.out.println(ex.getMessage());            15         }16     }17 }18 class ZeroException extends Exception{    19     public ZeroException(String msg){20         super(msg);21     }22 }23 class ZerpSubException extends Exception{24     public ZerpSubException(String msg){25         super(msg);26     }27 }28 class NumberCheck{29     //除法30     public int division(int x,int y) throws ZeroException,ZerpSubException{31         if(y<0){32             throw new ZerpSubException("除数不能为负数");33         }34         if(y==0){35             throw new ZeroException("除数不能为0");36         }37         int m=x/y;38         return m;39     }40 }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表