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));
}
}
标签:Exception,MyMath,int,11.5,关键词,public,static,throws
From: https://www.cnblogs.com/pansidong/p/17520426.html