注册 登录
编程论坛 JAVA论坛

请问这个自定义异常的错误是什么?

黑曼巴爱编程 发布于 2016-09-29 14:22, 1940 次点击
class MyException extends Exception{
    public static void main(String[] args)
    {
           int id = 0;
           MyException(String message,int id){
               super(message);
               this.id = id;
           }
           
           int getId(){
               return id;
           }
    }
}
public class TestMyException{
    public void regist(int num) throws MyException{
        if(num < 0){
            throw new MyException("人数为负数,不合理",3);
        }  
        System.out.println("登记人数"+ num);
    }
    public void manager(){
        try{
            regist(100);
        } catch (MyException e)
        {
            System.out.println("登记失败,出错类型码=" + e.getId());
            e.printStackTrace();
        }
        System.out.println("操作结束");
    }
    public static void main(String[] args){
        TestMyException t = new TestException();
        t.manager();
    }
}
1 回复
#2
逆风而前2016-10-03 22:02
把错误信息粘贴出来,看看,才知道具体是哪一行代码出错。
1