当前位置:网站首页>2022/07/29 学习笔记 (day19)异常处理
2022/07/29 学习笔记 (day19)异常处理
2022-07-30 08:09:00 【激进的黄瓜】
一、什么是异常处理
异常处理又叫例外处理。 对程序进行异常处理有两个目的,一是发现程序中出现的异常,二是对出现的异常进行相对操作或通知程序员进行修改。 使用try-catch语句可以很好的完成这两个目的。 try-catch语句和其他控制语句一样也是具有基本语法格式的,他的基本语法格式为:
try{
可能出现异常的代码
}catch(异常类型1 引用){
//异常类型1的处理代码
}
1.1、实例:
public class Test2{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
try {
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();
}
}
}
1.2、处理多个异常:
在java中,也是可以处理多个异常,也就是一个try多个catch,需要注意的是当try语句块中的代码出现一个异常后,不管后面的代码是否发生异常都不会再继续运行,而是直接查找是否有相对应异常的catch语句。 如果有相对应的就执行该catch语句中的内容,如果没有,就会直接将该异常输出。 例如:
public class Test2 {
public void method() {// 定义被调用的方法
System.out.println("调用类中的方法");
}
public static void main(String[] args) {
// TryCatchTest3 tct=null; //创建空引用
Test2 tct = null;
int[] is = new int[3];
try {// 将可能发生异常的代码包含在此语句中
tct.method(); // 使用空引用调用方法
is[3] = 5;
} catch (NullPointerException e) {// 判断代码是否发生空指针异常
System.out.println("发生空指针异常");
} catch (ArrayIndexOutOfBoundsException e) {// 判断是否发生数组越界异常
System.out.println("发生数组越界异常");
}
}
}1.3、finally语句:
finally语句的使用是无论前面的代码是否发生异常,都会执行finally语句中的内容,一个异常处理语句中只能最多有一个finally语句,可以没有。 在实际开发中,finally语句是非常重要的。 例如数据库操作中,连接数据库时连接成功和失败都是有可能的,但是无论是否成功最后都是要关闭该连接的。 关闭连接的代码语句就可以放在finally语句中。
1.4、try-catch-finally语句的注意点:
try-catch-finally语句中也是可以存在不完整的形式,例如前面看到的没有finally语句的形式。 除此之外,catch语句也是可以省略的,让finally语句紧跟try语句。 但是finally语句和catch语句不能同时省略,如果省略,这样的异常处理是没有任何意义的。 当try-catch异常处理语句中具有多个catch语句时,必须将子类异常放在前面,将父类异常放在后面。 也可以说将范围小的异常放在前面,将范围大的异常放在后面。 例如Exception是所有异常的父类,而算术异常只对特定异常处理,所以必须将算术异常放在前面,将Exception异常放在后面。
二、Error:
Error类主要用于表示程序中致命错误,例如内存不足等严重错误,一般与硬件相关。 不需要对这种错误进行处理。
三、throw关键字:
异常的抛出throw,可分为两大类
(1)由系统自动抛出
(2)通过throw关键字将异常对象显示抛出
显示抛出异常从某种程度上实现了将处理异常的代码从正常流程代码中分离开了,使得程序的主线保证相对完整,同时增加了程序的可读性和可维护性。 异常沿着调用层次上抛出,交由调用它的方法来处理。 异常抛出的语法:
throw new 异常类();
其中异常类必须是throwable类和其子类
如果一个方法可以导致一个异常但不处理它,它必须指定这种行为以使方法的调用者可以保护它们自己而不发生异常。 要做到这点可以在方法声明中包含一个throws子句。 throws总是出现在一个方法头中,用来标明该成员方法可能抛出的各种异常。
public class Test2 {
public void method() throws RuntimeException {
try {
int[] is = new int[3]; // 定义一个数组长度为3的数组
is[3] = 5; // 设置索引为3的数组元素
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("发生数组越界异常");
throw e; // 显性抛出捕获到的异常
}
}
public static void main(String[] args) {
Test2 tt = new Test2();
try {
tt.method(); // 调用方法
} catch (RuntimeException e) {// 对方法中抛出的异常进行处理
System.out.println("对异常再处理");
}
}
}边栏推荐
- 集合相关Collection
- 编程界的“躲猫猫”比赛 | 每日趣闻
- ACL 2022 | 引入角度margin构建对比学习目标,增强文本语义判别能力
- 电源完整性基础知识
- tabindex attribute of input tag & tabindex attribute of a tag
- 【WeChat Mini Program】Page Events
- ant-design form form verification upload component (with personal packaged upload component)
- 瑞吉外卖项目(五) 菜品管理业务开发
- Golang DES 加解密如何实现?
- 内卷下的智能投影行业,未来何去何从?
猜你喜欢

编程界的“躲猫猫”比赛 | 每日趣闻

typescript7-typescript common types

【Flask框架②】——第一个Flask项目

PCB板加工流程中哪些因素会影响到传输线阻抗

Alibaba Cloud Cloud Server Firewall Settings

Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises
![[Mini Program Column] Summarize the development specifications of uniapp to develop small programs](/img/7b/110d324eba00652e4987bc623a5bc6.png)
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs

typescript6 - simplify the steps to run ts

电脑文档误删除怎么恢复,恢复误删除电脑文档的方法

剖析SGI STL空间配置器(一 、辅助接口函数)
随机推荐
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs
积分专题笔记-积分的定义
typescript4 - installs a toolkit for compiling ts
The blockbuster IP that has been popular in the world for 25 years, the new work has become a script paradise
Scala
ant-design form form verification upload component (with personal packaged upload component)
C语言经典练习题(3)——“汉诺塔(Hanoi)“
SEER数据库中“手术变量(RX SUMM-SURG OTH REG/DIS )”下的字段解释
Field interpretation under "Surgical variables (RX SUMM-SURG OTH REG/DIS)" in SEER database
【Kotlin 中的类和继承】
Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises
SwiftUI SQLite 教程之 构建App本地数据库实现创建、读取、更新和删除(教程含完成项目源码)
Lenovo Notebook How to Change Windows 10 Boot Logo Icon
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
涛思 TDengine 2.6+优化参数
test111
Farthest Point Sampling - D-FPS vs F-FPS
Activating data potential Amazon cloud technology reshapes cloud storage "family bucket"
深入浅出零钱兑换问题——背包问题的套壳
typescript7-typescript common types