当前位置:网站首页>Exceptions and exception handling mechanisms
Exceptions and exception handling mechanisms
2022-08-11 00:32:00 【Weak crown original intention】
1.什么是异常
Unusual events that occur during program operation,导致程序终止运行
2.Java中如何进行异常处理
Java的异常处理是通过5个关键字来实现的:try、catch、 finally、throw、throws
3.使用try-catch块捕获异常,分为三种情况
第一种情况 :normal first case :
正常
public void method(){
try {
// 代码段(此处不会产生异常)
} catch (异常类型 ex) {
// 对异常进行处理的代码段
}
// 代码段
}
第二种情况:出现异常
\
public void method(){
try {
// 代码段 1
// 产生异常的代码段 2
// 代码段 3
} catch (异常类型 ex) {
// 对异常进行处理的代码段4
}
// 代码段5
}
第三种情况:异常类型不匹配
public void method(){
try {
// 代码段 1
// 产生异常的代码段 2
// 代码段 3
} catch (异常类型 ex) {
// 对异常进行处理的代码段4
}
// 代码段5
}
4.printStackTrace()The stack trace function of the program shows the execution flow of the program running to the current class
异常对象常用的方法
方法 | 说明 |
void printStackTrace() | 输出异常的堆栈信息 |
String getMessage() | 返回异常信息描述字符串, 是printStackTrace()输出信息的一部分 |
常见的异常类型
异常类型 | 说明 |
Exception | 异常层次结构的父类 |
ArithmeticException | 算术错误情形,如以零作除数 |
ArrayIndexOutOfBoundsException | 数组下标越界 |
NullPointerException | 尝试访问 null 对象成员 |
ClassNotFoundException | 不能加载所需的类 |
IllegalArgumentException | 方法接收到非法参数 |
ClassCastException | 对象强制类型转换出错 |
NumberFormatException | 数字格式转换异常,如把"abc"转换成数字 |
在try-catch块后加入finally块
public void method(){
try {
// 代码段 1
// 产生异常的代码段 2
} catch (异常类型 ex) {
// 对异常进行处理的代码段3
return;
}finally{
// 代码段 4
}
}
5.异常体系结构
6.自定义异常
Steps to use custom exceptions
边栏推荐
- Design and Realization of Employment Management System in Colleges and Universities
- Kunpeng compilation and debugging and basic knowledge of native development tools
- 图像识别和语义分割的区别
- 什么是“门”电路(电子硬件)
- Dump file generation, content, and analysis
- sqlmap combined with dnslog fast injection
- 英文文献阅读时,如何做笔记?
- C# JObject解析JSON数据
- 镜头之滤光片---关于日夜两用双通滤光片
- HW-蓝队工作流程(1)
猜你喜欢
单片机人机交互--矩阵按键
Software Testing Certificate (1) - Software Evaluator
嵌入式软件打log的一些心得
[Excel knowledge and skills] Convert text numbers to numeric format
盘点美军的无人机家底
91.(cesium之家)cesium火箭发射模拟
EN 12467纤维水泥平板产品—CE认证
学习Apache ShardingSphere解析器源码(一)
How to do patent mining, the key is to find patent points, in fact, it is not too difficult
地下管廊可视化管理系统搭建
随机推荐
Apache Commons Configuration远程代码执行漏洞(CVE-2022-33980)分析&复现
如何破坏Excel文件,让其显示文件已损坏方法
两个链表的第一个公共节点——LeetCode
nodejs项目连接mysql数据库
Ali P7 bask in January payroll: hard to fill the, really sweet...
Server Tips
SQL injection base
英文文献阅读时,如何做笔记?
11. Custom Converter
容器技术真的是环境管理的救星吗?
[21-day learning challenge - kernel notes] (5) - devmem read and write register debugging
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
构建检测,无规矩不成方圆
全排列思路详解
10. Notes on receiving parameters
Design and Realization of Employment Management System in Colleges and Universities
如何做专利挖掘,关键是寻找专利点,其实并不太难
分库分表ShardingSphere-JDBC笔记整理
Dump file generation, content, and analysis
J9数字论:DAO治理更像一种生态过程:治理原生于网络,不断演变