不確定我的理解是否正確
我知道try catch 可以把例外接住,讓後續的程式可以繼續執行
但是throw 還有 throws的用途就不太知道了
比如說我有一個aMethod 拋出例外
public class Throw {
public static int aMethod (int a,int b) throws Exception {
return a/b;
}
public static void main(String[] args) throws Exception {
int b=aMethod(5,0);
System.out.println("mike");
}
}
阿我有沒有加throws 都是一樣後面的mike都不會印出來阿
還是說這是用來提醒呼叫 aMethod的函數 你要記得用try catch 包住喔
是這樣嗎
另外網路上也有提到像是jdbc的例外 sqlexception是我們無法處理的
但既然我們無法處理 加了throw關鍵字後 jvm是會幫我們幹嘛?
public class Throw {
public static int aMethod (int a,int b) throws Exception {
return a/b;
}
public static void main(String[] args) {
try {
int b=aMethod(5,0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("mike");
}
}