当前位置:网站首页>2022/07/29 Study Notes (day19) Exception Handling
2022/07/29 Study Notes (day19) Exception Handling
2022-07-30 09:31:00 【aggressive cucumber】
一、什么是异常处理
异常处理又叫例外处理. 对程序进行异常处理有两个目的,一是发现程序中出现的异常,二是对出现的异常进行相对操作或通知程序员进行修改. 使用try-catchStatements serve both purposes well. try-catchStatements have the same basic syntax as other control statements,Its basic grammar format is :
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中,It is also possible to handle multiple exceptions,也就是一个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语句中的内容,There can be at most one exception handling statementfinally语句,可以没有. 在实际开发中,finally语句是非常重要的. 例如数据库操作中,连接数据库时连接成功和失败都是有可能的,但是无论是否成功最后都是要关闭该连接的. 关闭连接的代码语句就可以放在finally语句中.
1.4、try-catch-finally语句的注意点:
try-catch-finallyIncomplete forms can also exist in sentences,such as the one seen beforefinally语句的形式. 除此之外,catch语句也是可以省略的,让finally语句紧跟try语句. 但是finally语句和catch语句不能同时省略,如果省略,Such exception handling is meaningless. 当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("对异常再处理");
}
}
}
边栏推荐
- BaseQuickAdapter方法getBindingAdapterPosition
- Integral Topic Notes - Path Independent Conditions
- DDR、GDDR、QDR的区别
- Concise Notes on Integrals - Types of Curve Integrals of the Second Kind
- 342 · Valley Sequence
- HashSet和LinkedHashSet
- 英语语法-名词性从句
- Leetcode - 990: equations of satisfiability
- 用示波器揭示以太网传输机制
- 积分简明笔记-第一类曲线积分的类型
猜你喜欢
leetcode力扣——一篇文章解决多数之和问题
激活数据潜力 亚马逊云科技重塑云上存储“全家桶”
Excel xlsx file not supported两种解决办法【杭州多测师】【杭州多测师_王sir】
信号完整性测试
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs
【无标题】
【SQL server速成之路】——身份验证及建立和管理用户账户
怎么在本地电脑上运行dist文件
hcip 第14天学习笔记
随机推荐
浅论各种调试接口(JTAG、SWD、RDI、Jlink、Ulink、STlink)的区别
电源完整性基础知识
开关电源波纹的产生、测量及抑制,一篇全搞定!
SQL window function
HashSet和LinkedHashSet
function (1)
Concise Notes on Integrals - Types of Curve Integrals of the First Kind
剖析SGI STL空间配置器(一 、辅助接口函数)
网络/信息安全顶刊及相关期刊会议
与tcp协议有关的几个知识点
Circuit analysis: constant current source circuit composed of op amp and triode
【Flask框架①】——Flask介绍
MySQL Explain 使用及参数详解
最远点采样 — D-FPS与F-FPS
积分专题笔记-与路径无关条件
342 · 山谷序列
It is said that FPGA is high-end, what can it do?
test4
MongoDB - 千万级数据脚本过滤笔记
TreeSet解析