当前位置: 首页 > ds >正文

JAVA笔记6——异常

目录

1、异常的概念 

 1-1、Throwable类的继承体系、错误类和异常类的介绍

 2、运行时异常与编译时异常

2-1、两种异常的概念​编辑

 2-2、常见的运行时异常

 3、异常处理及语法

3-1、处理异常的五个关键字

3-2、try……catch语句 

3-3、finally语句 

4、抛出异常

4-1、throws关键字 

 4-2、throw关键字

 5、自定义异常类​编辑

6、本章小结 


1、异常的概念 


public class Example01 {public static void main(String[] args) {int result = divide(4, 0); // 调用 divide() 方法,第 2 个参数为 0System.out.println(result);}// 下面的方法实现了两个整数相除public static int divide(int x, int y) {int result = x / y; // 定义变量 result 记录两个数相除的结果return result; // 将结果返回}
}

 1-1、Throwable类的继承体系、错误类和异常类的介绍

 2、运行时异常与编译时异常

2-1、两种异常的概念

 2-2、常见的运行时异常

 3、异常处理及语法

3-1、处理异常的五个关键字

3-2、try……catch语句 

class Example02 {public static void main(String[] args) {//下面的代码定义了一个 try…catch 语句用于捕获异常try {int result = divide(4, 0);    //调用 divide() 方法System.out.println(result);} catch (Exception e) {         //对异常进行处理System.out.println("捕获的异常信息为:" + e.getMessage());}System.out.println("程序继续向下执行…");}//下面的方法实现了两个整数相除public static int divide(int x, int y) {int result = x / y;              //定义变量 result 记录两个数相除的结果return result;                   //将结果返回}
}

         catch代码对异常处理完毕后,程序仍会向下执行,而不会终止。

        需要注意的是,在try代码块中,发生异常的语句后面的代码是不会执行的,例如,上面代码中的第六行的代码(打印语句)就没有执行。

3-3、finally语句 

public class Example03 {public static void main(String[] args) {//下面的代码定义了一个 try…catch…finally 语句用于捕获异常try {int result = divide(4, 0);    //调用 divide() 方法System.out.println(result);} catch (Exception e) {         //对捕获的异常进行处理System.out.println("捕获的异常信息为:" + e.getMessage());return;                      //用于结束当前语句} finally {System.out.println("进入 finally 代码块");}System.out.println("程序继续向下...");}//下面的方法实现了两个整数相除public static int divide(int x, int y) {int result = x / y;              //定义变量 result 记录两个数相除的结果return result;                   //将结果返回}
}

        去掉第9行的return,第13行代码就会执行;return若不去掉,执行完finally中的语句后程序就结束了。 

4、抛出异常

4-1、throws关键字 

public class Example04 {public static void main(String[] args) {int result = divide(4, 2);      //调用 divide() 方法System.out.println(result);}//下面的方法实现了两个整数相除,并使用 throws 关键字声明抛出异常public static int divide(int x, int y) throws Exception {int result = x / y;             //定义变量 result 记录两个数相除的结果return result;                   //将结果返回}
}

在throws关键字声明了该方法可能会抛出异常后,没有处理异常,运行代码后会出现以下的错误。

 修改代码如下,使用try……catch语句处理divide()方法抛出的异常

class Example05 {public static void main(String[] args) {//下面的代码定义了一个 try…catch 语句用于捕获异常try {int result = divide(4, 2);    //调用 divide() 方法System.out.println(result);} catch (Exception e) {          //对捕获的异常进行处理e.printStackTrace();          //打印捕获的异常信息}}// 下面的方法实现了两个整数相除,并使用 throws 关键字声明抛出异常public static int divide(int x, int y) throws Exception {int result = x / y;                //定义变量 result 记录两个数相除的结果return result;                     //将结果返回}}

修改后的代码可正常运行 

输出结果:2

public class Example06 {public static void main(String[] args) throws Exception { //调用 divide() 方法int result = divide(4, 0);System.out.println(result);}//下面的方法实现了两个整数相除,并使用 throws 关键字声明抛出异常public static int divide(int x, int y) throws Exception {int result = x / y;                //定义变量 result 记录两个数相除的结果return result;                     //将结果返回}
}

由输出结果可以看出,在运行时由于没有对”/by zero“的异常进行处理,最终导致程序终止。 

 4-2、throw关键字

class Example07 {// 定义 printAge() 输出年龄public static void printAge(int age) throws Exception {if(age <= 0) {// 对业务逻辑进行判断,当输入年龄为负数时抛出异常throw new Exception("输入的年龄有误,必须是正整数!");} else {System.out.println("此人年龄为:" + age);}}public static void main(String[] args) {// 下面的代码定义了一个 try...catch 语句用于捕获异常int age = -1;try {printAge(age);} catch (Exception e) {// 对捕获的异常进行处理System.out.println("捕获的异常信息为:" + e.getMessage());}}
}

 5、自定义异常类

class DivideByMinusException extends Exception {public DivideByMinusException () {super(); //调用 Exception 无参的构造方法}public DivideByMinusException (String message) {super(message); //调用 Exception 有参的构造方法}
}class Example08 {public static void main(String[] args) {int result = divide(4, -2);System.out.println(result);}//下面的方法实现了两个整数相除public static int divide(int x, int y) {if(y<0){throw new DivideByMinusException("除数是负数"); //异常的实例化对象}int result = x / y; //定义变量 result 记录两个数相除的结果return result; //将结果返回}
}

class DivideByMinusException extends Exception {public DivideByMinusException () {super(); //调用 Exception 无参的构造方法}public DivideByMinusException (String message) {super(message); //调用 Exception 有参的构造方法}
}class Example09{public static void main(String[] args) {//下面的代码定义了一个 try…catch 语句用于捕获异常try {int result = divide(4, -2);System.out.println(result);} catch (DivideByMinusException e) { //对捕获的异常进行处理System.out.println(e.getMessage()); //打印捕获的异常信息}}//下面的方法实现了两个整数相除,并使用 throws 关键字声明抛出自定义异常public static int divide(int x, int y) throws DivideByMinusException{if (y < 0) {throw new DivideByMinusException("除数是负数");//用new创建实例对象}int result = x / y; //定义变量 result 记录两个数相除的结果return result; //将结果返回}
}

6、本章小结 

http://www.xdnf.cn/news/5655.html

相关文章:

  • 【我的创作纪念日】512
  • Error from server (NotFound) namespaces kubesphere-system not found报错解决方案
  • 解锁仓储升级:Canopen到Profinet网关的革新应用!
  • 连续抵消解码器--Successive Cancellation decoder(SC 解码器)
  • MNIST 数据并行 Data Parallel - DP
  • P4933 大师
  • ROS 2 FishBot PID控制电机代码
  • ‌中继器:网络中的“血包”与“加时器”‌
  • 【python编程从入门到到实践】第六章 字典
  • 将PyQt5设计的程序打包成.exe文件
  • 掌握 void 类型在函数返回值中的应用
  • 企业级数据安全实践:ChatBI的多源异构治理与OLAP查询加速方案
  • Java中的JDK7和JDK8时间类详解
  • Zotero文献管理
  • Nginx重写功能
  • 使用Python调用ComfyUI API实现图像生成
  • Java+MySQL学生管理系统
  • 【github分享】开发者学习路线图
  • DBdoctor:一款企业级数据库性能诊断工具
  • 什么是 ANR 如何避免它
  • Java 程序流程控制篇
  • 什么是电路耦合以及如何解耦合
  • PostgreSQL 的 pg_column_size 函数
  • 《打造自己的DeepSeek》第2期:怎么安装自己的DeepSeek?
  • 当 Manus AI 遇上 OpenAI Operator,谁能更胜一筹?
  • Python 对象引用、可变性和垃圾 回收(标识、相等性和别名)
  • python 写一个工作 简单 番茄钟
  • Linux-Ubuntu安装Stable Diffusion Forge
  • 【计组】真题
  • 快速傅里叶变换暴力涨点!基于时频特征融合的高创新时间序列分类模型