Java基础--异常处理

时间:2023-03-09 22:34:10
Java基础--异常处理

1.异常的传统处理方式

缺点:

[1] 通过判断影响执行效率。

[2] 判断逻辑和业务逻辑交织在一起,可维护性很差。

 public class Test01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:"); int num1 = 0;
if(sc.hasNextInt()) {
num1 = sc.nextInt(); System.out.println("请输入第二个数:");
int num2 = 0;
if(sc.hasNextInt()) {
num2 = sc.nextInt(); if(0 == num2) {
System.out.println("除数不能为0!");
}else {
int r = num1 / num2;
System.out.println("num1/num2 = "+r);
} }else {
System.out.println("第二个数输入不是数字");
} }else {
System.out.println("第一个数输入不是数字!");
}
}
}

2.Java异常处理的定义

异常是指在程序的运行过程中所发生的不正常的情况,它会中断正在运行的程序。

3.Java异常处理机制

java中通过异常处理机制为程序提供异常处理的能力,保持程序继续运行而不中断!

Java基础--异常处理

4.try/catch

把有可能产生异常的代码放到try代码块中,catch代码块负责捕获并处理异常。

[1]正常执行,没出现任何异常

Java基础--异常处理

[2]出现异常,异常处理,正常结束

Java基础--异常处理

[3]异常类型不匹配

Java基础--异常处理

[4] 多重catch

 public class Test03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:"); int num1 = 0;
int num2 = 0; try {
num1 = sc.nextInt(); System.out.println("请输入第二个数:");
num2 = sc.nextInt(); int r = num1 / num2;
System.out.println("num1/num2 = " + r);
}catch (ArithmeticException e) {
System.out.println("数学计算异常:"+e.getMessage());
}catch(InputMismatchException e) {
System.out.println("输入不匹配异常:"+e.getMessage());
}catch (Exception e) {
System.out.println("发送异常:"+e.getMessage());
} System.out.println("程序正常结束");
}
}

5.try/catch/finally

把有可能产生异常的代码放到try中,catch负责匹配并处理异常,finally块用于进行收尾工作(关闭数据库、关闭文件、释放内存等资源),不管是否发生异常,finally都执行。

 public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:"); int num1 = 0;
int num2 = 0; try {
num1 = sc.nextInt(); System.out.println("请输入第二个数:");
num2 = sc.nextInt(); int r = num1 / num2;
System.out.println("num1/num2 = " + r);
} catch (Exception e) {
System.out.println("程序出现异常");
} finally {
System.out.println("不管是否出现异常,finally都执行");
} System.out.println("程序正常结束");
}

6.异常的分类

Exception 根据是否处理分为两种情况。

RuntimeException:运行时异常。不要求程序必须做出处理。是所有运行时异常的父类。

CheckedException:检查时异常。要求程序必须处理,不处理编译不通过。

7.声明异常(throws关键字)

当一个方法可能存在异常,而此时自身又无法更好的处理,可以交给外界处理。此时用throws声明并抛出异常。

如果调用处也不知道如何处理异常,可选择继续声明异常,我们把这个过程称为异常上抛。

 public class Test01 {

     public static int div(int a, int b) throws ArithmeticException{
int r = 0;
r = a / b;
return r;
} public static void main(String[] args) {
try {
Test01.div(10, 0);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}
}
}

8.手动抛出异常(throw关键字)

除了系统自动抛出异常外,有些问题需要开发者手动抛出异常,使用关键字throw。

 package cn.sxt02.exception06;

 public class Student {
private String name;
private String gender; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getGender() {
return gender;
} public void setGender(String gender) throws Exception{
if(gender.equals("男") || gender.equals("女")) {
this.gender = gender;
}else {
throw new Exception("性别不合法!");
}
} public Student(String name, String gender) {
super();
this.name = name;
this.gender = gender;
} public Student() {
super();
} }
 public class Test01 {
public static void main(String[] args){
Student stu = new Student();
stu.setName("二狗");
try {
stu.setGender("xxx");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

9.自定义异常

如果开发者需要手动抛出的异常在系统不存在,可以自定义异常。

如果要自定义异常,首先要确定异常类型,如果异常是运行时异常,必须继承RuntimeException或其子类;如果异常是检查时异常,必须继承Exception或其子类。

异常的命名方式,参考系统命名方式,以Exception结尾。

 public class AgeException extends Exception{

     public AgeException() {
super();
} public AgeException(String message) {
super(message);
} }