当前位置:网站首页>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
边栏推荐
- WebView2 通过 PuppeteerSharp 实现RPA获取壁纸 (案例版)
- 【pypdf2】安装、读取和保存、访问页面、获取文本、读写元数据、加密解密
- 线上突然查询变慢怎么核查
- “蔚来杯“2022牛客暑期多校训练营3 DF题解
- “蔚来杯“2022牛客暑期多校训练营4 ADHK题解
- [Excel知识技能] 将“假“日期转为“真“日期格式
- [21-day learning challenge - kernel notes] (5) - devmem read and write register debugging
- 时间戳转换为日期格式、获取当前时间戳
- 学习Apache ShardingSphere解析器源码(一)
- SQL injection base
猜你喜欢
镜头之滤光片---关于日夜两用双通滤光片
软件测试证书(1)—— 软件评测师
单片机人机交互--矩阵按键
[Excel knowledge and skills] Convert numeric format numbers to text format
百战RHCE(第四十八战:运维工程师必会技-Ansible学习3-构建Ansible清单)
李彦宏拆墙交朋友,大厂“塑料友情”能否帮百度啃下硬骨头?
word 设置标题前分页
深度解析volatile关键字(保证够全面)
② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
有哪些可以投稿软件工程/系统软件/程序设计语言类外文期刊、会议?
随机推荐
“蔚来杯“2022牛客暑期多校训练营4 ADHK题解
input输入框超出部分用省略号表示以及判断内容是否有超出(PC端)
20张图,全面掌握MVCC原理!
图像识别和语义分割的区别
CF1534F2-Falling Sand (Hard Version)
What is the ASIO4ALL
Where can I download IEEE papers?
分布式.性能优化
时间戳转换为日期格式、获取当前时间戳
Which foreign language journals and conferences can be submitted for software engineering/system software/programming language?
[21-day learning challenge - kernel notes] (5) - devmem read and write register debugging
Pico 4更多参数曝光:Pancake+彩色透视,还有Pro版本
镜头之滤光片---关于日夜两用双通滤光片
SQL injection base
YOLOv5的Tricks | 【Trick13】YOLOv5的detect.py脚本的解析与简化
Introduction to Qt (6) - Implementation of the lottery system
YOLOv5的Tricks | 【Trick12】YOLOv5使用的数据增强方法汇总
Web-based meal ordering system in epidemic quarantine area
[Excel知识技能] 将“假“日期转为“真“日期格式
报错:Client does not support authentication protocol requested by server; consider upgrading MySQL cli