1.使用說明
在程序執行中,除了自動拋出異常對象的狀況以外,咱們還能夠手動的throw一個異常類的對象。面試
2.[面試題]
throw 和 throws區別:
throw 表示拋出一個異常類的對象,生成異常對象的過程。聲明在方法體內。
throws 屬於異常處理的一種方式,聲明在方法的聲明處。ide
3.典型例題this
class Student{ private int id; public void regist(int id) throws Exception { if(id > 0){ this.id = id; }else{ //手動拋出異常對象 // throw new RuntimeException("您輸入的數據非法!"); // throw new Exception("您輸入的數據非法!"); throw new MyException("不能輸入負數"); } } @Override public String toString() { return "Student [id=" + id + "]"; } }