RuntimeException 屬于運(yùn)行時(shí)期異常屏积,與NullPointerException,ArrayIndexOutOfBoundsException異常 一樣虎敦,不需要用try...catch...來捕獲,調(diào)用者無須處理此異常蚓再,需要修改代碼绎秒。它是終止程序的一種異常
···
public class RuntimeExceptionDemo {
public static void main(String[] args) {
double d=getArea(-1);
System.out.println(d);
}
public static double getArea(double r) {
if(r<=0)
throw new RuntimeException("半徑錯(cuò)誤");
return r*r*Math.PI;
}
}
···
Exception in thread "main" java.lang.RuntimeException: 半徑錯(cuò)誤
at com.lesson06.RuntimeExceptionDemo.getArea(RuntimeExceptionDemo.java:13)
at com.lesson06.RuntimeExceptionDemo.main(RuntimeExceptionDemo.java:6)