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; //将结果返回}
}