不确定我的理解是否正确
我知道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");
}
}