← Java OOP Knowledge Map← Java OOP 知识地图
Exceptions

Exceptions

An exception is an object thrown when normal control flow is disrupted. Learn who throws, who catches, and which exceptions must be handled.Exception 是程序正常流程被打断时抛出的对象。你要知道什么时候 throw,谁 catch,以及 checked 和 unchecked 的区别。

throw / catch / finally

The direction exceptions travel异常的流动方向

try {
    readFile("data.txt");
} catch (IOException e) {
    System.out.println("File read failed");
} finally {
    closeResource();
}
Checked vs Unchecked

Whether the compiler forces handling编译器会不会逼你处理

Type类型Example例子Meaning怎么理解
Checked exceptionIOExceptionExternal failures; the compiler requires catch or throws.外部环境导致的失败,编译器要求 catch 或 throws。
Unchecked exceptionNullPointerExceptionUsually code bugs or invalid calls; not forced by the compiler.通常是代码 bug 或非法调用,不强制 catch。