首页 > 其他 > 详细

手动抛出

时间:2016-05-03 20:13:50      阅读:241      评论:0      收藏:0      [点我收藏+]
package exer;
//手动的抛出一个异常的格式
public class MyException extends Exception{
    static final long serialVersionUTD = -703489545;
    public MyException(){
        
    }
    public MyException(String msg){
        super(msg);
    }
}






package exer;

public class TestStudent {
    public static void main(String[] args){
        Student s=new Student();
        try{
            s.regist(-12);
        }catch(MyException e){
            System.out.println(e.getMessage());
        }
        System.out.println(s);
    }    
}

class Student{
    private int id;
    public void regist(int id) throws MyException{
        if(id>0)
            this.id=id;
        else {
            throw new MyException("输入的学号有误");
        }
    }
    public String toString() {
        return "Student [id=" + id + "]";
    }
    
} 

 

手动抛出

原文:http://www.cnblogs.com/alhh/p/5456246.html

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