11.5 throws关键词
demo1
class MyMath {
public static int div(int x,int y) throws Exception {
return x / y;
}
}
public class JavaDemo {
public static void main(String args[]) {
try {//调用throws方法时,需要进行异常处理,否则报错
System.out.println(MyMath.div(10,2));
} catch (Exception e){
e.printStackTrace();
}
}
}
demo2 在主方法中继续抛出异常
class MyMath {
public static int div(int x,int y) throws Exception {
return x / y;
}
}
public class JavaDemo {
// 主方法中使用throws继续抛出可能产生的异常,一旦出现异常交jvm进行默认异常处理---下面就不用try,catch了
public static void main(String args[]) throws Exception{
System.out.println(MyMath.div(10,0));
}
}
相信坚持的力量,日复一日的习惯.