Throws與Throw

 throws  使用在函數上, 後面跟的是異常類.可以跟多個,用逗號隔開

throw使用在函數內, 後跟的是異常對象

 

class Demo {
         public int div(int a, int b) throws NegativeException {
                if (b < 0) {
                   throw new NegativeException("除數是負數了");
            }
                return a / b;
       }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章