45 lines
1.4 KiB
Markdown
45 lines
1.4 KiB
Markdown
# 异常
|
||
|
||
分为Error 和 Exception
|
||
|
||
其中Exception中 又分为两类,如图中所示
|
||
|
||
<img src="https://gitee.com/icecat2233/picture/raw/master/20250414182933522.png" alt="image-20250414182932233" style="zoom: 33%;" />
|
||
|
||
其中两种错误的表现形式有下列
|
||
|
||
|
||
|
||

|
||
|
||
<img src="https://gitee.com/icecat2233/picture/raw/master/20250414182755030.png" alt="image-20250414182746446" style="zoom: 67%;" />
|
||
|
||
# 异常的两种处理方式
|
||
|
||
1. **try...catch...捕获异常**
|
||
|
||
<img src="https://gitee.com/icecat2233/picture/raw/master/20250416155305609.png" alt="image-20250416155256665" style="zoom: 50%;" />
|
||
|
||
2. **throws抛出异常**
|
||
|
||
<img src="https://gitee.com/icecat2233/picture/raw/master/20250416155817373.png" alt="image-20250416155816351" style="zoom:50%;" />
|
||
|
||
## 两种方式怎么选择:
|
||
|
||
思路:这个问题是否需要暴漏出来?
|
||
需要暴漏,用throws
|
||
|
||
不需要暴漏,用try catch
|
||
|
||
## throws和throw的区别
|
||
|
||
<img src="https://gitee.com/icecat2233/picture/raw/master/20250416155752416.png" alt="image-20250416155750720" style="zoom:50%;" />
|
||
|
||
# 自定义异常
|
||
|
||
<img src="https://gitee.com/icecat2233/picture/raw/master/20250416163826822.png" alt="image-20250416163824387" style="zoom:50%;" />
|
||
|
||
# 异常中的细节
|
||
|
||
<img src="https://gitee.com/icecat2233/picture/raw/master/20250416164540545.png" alt="image-20250416164538085" style="zoom:50%;" />
|