当前位置:网站首页>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
边栏推荐
- C# JObject解析JSON数据
- word 设置标题前分页
- How engineers treat open source
- Design and Realization of Employment Management System in Colleges and Universities
- 有哪些可以投稿软件工程/系统软件/程序设计语言类外文期刊、会议?
- What is the ASIO4ALL
- SQL injection base - order by injection, limit, wide byte
- 什么是“门”电路(电子硬件)
- 【考虫 六级英语】语法课笔记
- In 22 years, the salary of programmers nationwide in January was released, only to know that there are so many with annual salary of more than 400,000?
猜你喜欢
随机推荐
Software protection scenario of NOR FLASH flash memory chip ID application
sqlmap combined with dnslog fast injection
② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
构建检测,无规矩不成方圆
有哪些可以投稿软件工程/系统软件/程序设计语言类外文期刊、会议?
11. Custom Converter
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 2 DGHJKL Problem Solution
YOLOv5的Tricks | 【Trick12】YOLOv5使用的数据增强方法汇总
LENS CRA和SENSOR CRA匹配问题解析
2022.8.10-----leetcode.640
单片机人机交互--矩阵按键
[Excel知识技能] 将“假“日期转为“真“日期格式
盘点美军的无人机家底
线上突然查询变慢怎么核查
Server Tips
“蔚来杯“2022牛客暑期多校训练营3 DF题解
C# JObject解析JSON数据
How to do patent mining, the key is to find patent points, in fact, it is not too difficult
Difference Between Image Recognition and Semantic Segmentation
rhel7.0解决yum无法使用(system is not registered to Red Hat Subscription Management)









