首页 > 编程语言 > 详细

java中如何制定自定义异常

时间:2015-03-27 16:53:25      阅读:213      评论:0      收藏:0      [点我收藏+]
 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 }

 

java中如何制定自定义异常

原文:http://www.cnblogs.com/guoyansi19900907/p/4371924.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!