当前位置:网站首页>受检异常和非受检异常的区别和理解
受检异常和非受检异常的区别和理解
2022-07-06 09:20:00 【快醒醒鸭今天你编程了吗?】
1、受检异常
所谓的受检异常其实表示的是在编译的时候,要强制检查的异常,这种异常需要去显示的通过try/catch来进行捕获或者通过throws去抛出去否则程序无法通过编译的。
2、非受检异常
所谓的非受检异常表示编译器可以不需要去强制去检查异常,这种异常不需要去显示去捕获或者抛出。
在java里面所有的异常都需要继承java.lang.Throwable这个类如下图:

Throwable有两个直接的子类:
- Error:表示的是程序底层或者硬件层面的错误,比如像常见的OOM异常(内存溢出异常)那么这种异常和程序本身没有什么关系所以它不需要去检查属于非受检异常。
- Exception:表示的是程序里面的一些异常,可能是由于程序不严谨导致的比如像NullPointerException(空指针异常)等,Exception派生出两种异常类型
- RuntimeException:运行时异常属于非受检异常
- 其他Exception
小结:
所以除了Error和RuntimeException以及RuntimeException的派生类以外,其他的异常都是属于受检异常比如:IOException和SQLException,其实之所以在Java里面去设计一些强制检查的异常我认为主要的原因是:
考虑程序的正确性、稳定性、可靠性,如上述说的数据库异常是程序无法提前预料的异常但是一旦出现问题就会造成资源上的一个占用导致程序出现一些问题,所以这些问题我们要去捕获一旦出现问题可以及时做出相应的处理,如我们进行数据库操作try/catch之后会finally关闭xxx.close关闭连接,所以总的来说受检异常是程序中无法去判断的异常我们去用try/catch进行捕获。
边栏推荐
- vector
- Design a key value cache to save the results of the most recent Web server queries
- Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
- JS interview questions (I)
- Inheritance and polymorphism (Part 2)
- Aurora system model of learning database
- TYUT太原理工大学2022数据库之关系代数小题
- Network layer 7 protocol
- 初识指针笔记
- 1.C语言矩阵加减法
猜你喜欢

E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology

12 excel charts and arrays

5. Function recursion exercise

MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景

C语言实现扫雷游戏(完整版)

(超详细二)onenet数据可视化详解,如何用截取数据流绘图

Database operation of tyut Taiyuan University of technology 2022 database

TYUT太原理工大学2022“mao gai”必背

4. Binary search

5. Download and use of MSDN
随机推荐
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
String类
TYUT太原理工大学2022软工导论考试题型大纲
Rich Shenzhen people and renting Shenzhen people
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
西安电子科技大学22学年上学期《基础实验》试题及答案
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
5. Function recursion exercise
Branch and loop statements
12 excel charts and arrays
凡人修仙学指针-2
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
167. Sum of two numbers II - input ordered array - Double pointers
Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
继承和多态(上)
4.30动态内存分配笔记
最新坦克大战2022-全程开发笔记-2
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
4.30 dynamic memory allocation notes
Atomic and nonatomic