class MyMath{
public static int div(int x, int y) throws ArithmeticException{
return x/y;
}
}
public class ThrowsDemo {
public static void main(String[] args) {
System.out.println(MyMath.div(10,2));
}
}
只要使用了throws定义的方法都必须要去开发者手工处理,那么就太麻烦了。所以在设计的过程中,考虑到代码编写的方便,提供了一个灵活的可选的异常处理父类“RunTimeException”,这个类的异常子类可以不需要强制处理。
面试题:请解释RunTimeException与Exception的区别?
RunTimeException 是Exception的子类,RunTimeException 标准的异常可以不需要强制处理,Exception 的异常必须强制处理。
请列举RunTimeException的子类?
ArithmeticException NumberFormatException ClassCastException