·throws关键字主要用于方法声明上,指的是当前方法之中异常后交给被调用处处理;
范例1:
class MyMath {
public static int div(int x, int y) throws Exception {
return x / y;
}
}
public class Test {
public static void main(String[] args) {
try {
System.out.println("计算结果:"+MyMath.div(10, 2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
===========分割线===========
范例2:
class MyMath {
public static int div(int x, int y) throws Exception {
return x / y;
}
}
public class Test {
public static void main(String[] args) throws Exception {
System.out.println("计算结果:" + MyMath.div(10, 2));
}
}