当前位置:网站首页>受检异常和非受检异常的区别和理解
受检异常和非受检异常的区别和理解
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进行捕获。
边栏推荐
- IPv6 experiment
- 最新坦克大战2022-全程开发笔记-2
- (超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
- What are the advantages of using SQL in Excel VBA
- 初识C语言(上)
- 4.分支语句和循环语句
- Implement queue with stack
- View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
- Alibaba cloud microservices (IV) service mesh overview and instance istio
- C语言入门指南
猜你喜欢

最新坦克大战2022-全程开发笔记-1

7.数组、指针和数组的关系

MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series

Relational algebra of tyut Taiyuan University of technology 2022 database

Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes

TYUT太原理工大学2022软工导论大题汇总

继承和多态(上)

4.分支语句和循环语句

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

20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
随机推荐
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
凡人修仙学指针-1
Smart classroom solution and mobile teaching concept description
String class
TYUT太原理工大学往年数据库简述题
C language to achieve mine sweeping game (full version)
用栈实现队列
3. C language uses algebraic cofactor to calculate determinant
TYUT太原理工大学2022数据库大题之概念模型设计
Wei Pai: the product is applauded, but why is the sales volume still frustrated
Rich Shenzhen people and renting Shenzhen people
4. Binary search
Tyut Taiyuan University of technology 2022 introduction to software engineering
1.C语言矩阵加减法
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Introduction pointer notes
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
TYUT太原理工大学2022数据库大题之分解关系模式
IPv6 experiment
Database operation of tyut Taiyuan University of technology 2022 database