异常
异常顶层类Throwable
-
Error:大多数此类错误都是异常情况,程序无法触及的错误,不用处理
-
Exception:Exception和他所有非RuntimeException子类的异常都属于checked exception,这些异常都必须进行处理,throws或者try catch;
- RuntimeException:RuntimeException和它的所有子类都属于unchecked exception,程序不用去处理的异常
代码示例
下面两个方法
- checkedMethod:抛出Exception必须进行处理
- unCheckedMethod:抛出RuntimeException可以不用处理
public class TestException {
public static void checkedMethod() throws Exception {
throw new Exception("checked exception");
}
public static void unCheckedMethod() {
throw new RuntimeException("unChecked exception");
}
public static void main(String[] args) {
TestException.checkedMethod(); // Unhandled exception: java.lang.Exception
TestException.unCheckedMethod();
}
}