列出 5 个运行时异常¶
一、常见运行时异常¶
1. NullPointerException¶
最常见,访问 null 对象的方法/字段:
2. IllegalArgumentException¶
参数不合法:
3. IllegalStateException¶
状态不对:
4. IndexOutOfBoundsException¶
数组/集合越界:
List 是 IndexOutOfBoundsException。
5. ClassCastException¶
类型转换错误:
6. ArithmeticException¶
算术错误:
7. ConcurrentModificationException¶
迭代时修改集合:
8. UnsupportedOperationException¶
不支持的操作:
二、记忆口诀¶
NPE、IAE、ISE、IOOBE、CCE。
三、怎么避免¶
- 判空(Optional)。
- 参数校验(@NotNull)。
- 用迭代器或 removeIf 删除。
- 边界检查。
面试要求
题目只要 5 个,答出 NPE、IllegalArgumentException、IllegalStateException、IndexOutOfBoundsException、ClassCastException 即可。