hooyantsing's Blog

第31次课程

字数统计: 1.5k阅读时长: 7 min
2019/07/07

源辰74班

第31次课程

07.07.2019

**内容    **

Java_API_Exception处理[蒋斌]

1.异常处理

1.1try:异常捕获

仅写一个,最开始的位置。内部放入可能产生异常的代码。

1.2catch:捕获后解决异常

可写多个,可对捕获的不同异常进行处理。最后catch(Exception){代码},用于捕获未预期的异常的通用处理方式。

1.3flinally:最终

仅写一个,放在最后,无论是否报错都将执行内代码,通常用于写资源释放代码。

try{

**         可能产生异常的代码**

**   }catch(异常类型){**

**         异常后的处理代码**

**   }finally{**

**         //多用于资源释放的代码**

**         无论是否报异常,都将执行的代码**

**   }**

程序运行时产生的常见异常位于:RuntimeException

**8ad447710abb31a3bdca11276dcfbbf2.png

**

详见:JDK_API文档

异常(Exception)两类:运行异常 和 编译异常(非处理不可的异常)

**2.**ArithmeticException(算数运算异常)

1.当除数为0时,这种情况比较常见,所以我们在进行除法运算之前先判断下除数是否为0

2.如果运算的结果是无线循环的小数,并且在除的时候没有对结果设置精确的位数,这时就会抛出异常,这种情况比较容易被忽视,抛出异常后一般都会考虑是否为0,因此,要特别注意!!

3.当我们设置了结果的舍入模式是:ROUND_UNNECESSARY模式时,如果确保了计算的结果是精确的,则不会抛出异常,否则,就会抛出ArithmeticException异常!

实验代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.hooy.exception;
import java.util.Scanner;
public class SuanShu_exception {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入两个数字:");
       int a = sc.nextInt();
       int b = sc.nextInt();
       
       try {
           //尝试:放入可能报异常的代码
           System.out.println("值:"+a/b);
       }catch(ArithmeticException e) {  //ArithmeticException 异常的算数运算
           //出现异常则执行catch中的代码,否则不执行
           System.out.println("errer:除数不可为零");
       }
       System.out.println("程序正常结束");
    }
}

3.NullPointer****Exception(空指针异常)

4.ArrayIndexOutOfBoundsException(越界异常)

5.多异常捕获

实验代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.hooy.exception;
import java.util.Scanner;
public class Exception_Test {
    public static void main(String[] args) {
       int[] arr = new int[2];
       Scanner sc = new Scanner(System.in);
       try {
           int count = 0;
           while(count<=arr.length) {//<= 触发越界异常
               System.out.println("请输入第"+(count+1)+"个数");
               //第二次键入0      触发除数为零异常
               //键入非数字字符 触发数字转换异常
               arr[count] =  Integer.parseInt(sc.nextLine());
               count++;
           }
           System.out.println(arr[0]+"/"+arr[1]+"="+arr[0]/arr[1]);
           //不同的catch用于捕获不同的异常
       }catch(ArithmeticException e){
           System.out.println("除数为零异常");
       }catch(NumberFormatException e) {
           System.out.println("数字转化异常");
       }catch(ArrayIndexOutOfBoundsException e) {
           System.out.println("数组越界异常");
       }catch(Exception e) {//Exception等级最高,包含各种异常,因此放在最后作为捕获异常的最后防线
           System.out.println("最高级异常");
       }
       System.out.println("程序正常结束");
    }
}

6.throw(异常抛出

将异常抛给调用这个方法的类,让别人来处理。

单个代码块中仅一条throw,执行到throw即刻停止继续执行当前方法。

Class Person

//含有throw的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.hooy.exception;
public class Person {
    private String name;
    private int age;
    public Person() {}
    public Person(String name,int age) {
       this.name = name;
       this.age = age;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) throws  Exception {
       if(age<0 || age>200) {
           //将异常抛出,让调用者处理这个异常
           throw new Exception("年龄不合法");
       }
       this.age = age;
    }
    
}

Main Person_Test

//处理捕获Person类中的异常

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.hooy.exception;
public class Person_Test {
    public static void main(String[] args) {
       Person p = new Person();
       //由调用setAge方法的类来捕获异常,并做出处理方式
       try {
           p.setAge(-1);
       } catch (Exception e) {
           e.printStackTrace();
           System.out.println("异常");
       }
    }
}

7.自定义异常

自定义一个异常,并继承Exception(相当于隶属于Exception旗下)

**Class **AgeException

1
2
3
4
5
6
7
package com.hooy.exception;
public class AgeException extends Exception{
    public AgeException() {}
    public AgeException(String msg) {
       super(msg);
    }
}

Main Person

可将用于Person类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.hooy.exception;
public class Person {
    private String name;
    private int age;
    public Person() {}
    public Person(String name,int age) {
       this.name = name;
       this.age = age;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) throws  AgeException {
       if(age<0 || age>200) {
           //将异常抛出,让调用者处理这个异常
           throw new AgeException("年龄不合法");
       }
       this.age = age;
    }
}

8.关于异常的Demo

如果某异常继承RuntimeException,则该异常可以不被声明

**Class **LanPingException

1
2
3
4
5
6
7
package Demo;
public class LanPingException extends  Exception{
    public LanPingException() {}
    public LanPingException(String msg) {
       super(msg);
    }
}

**Class **MaoYanException

1
2
3
4
5
6
7
package Demo;
public class MaoYanException extends  Exception{
    public MaoYanException() {}
    public MaoYanException(String msg) {
       super(msg);
    }
}

**Class **NoPlanException

1
2
3
4
5
6
package Demo;
public class NoPlanException extends  Exception {
    public NoPlanException(String msg) {
       super(msg);
    }
}

Class Computer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package Demo;
public class Computer {
    private int state = 1;
    //电脑开机
    public void run() throws  LanPingException, MaoYanException {
       if(state==1) {
           throw new LanPingException("蓝屏故障");
       }
       if(state==2) {
           throw new MaoYanException("机器冒烟");
       }
       System.out.println("电脑正常运行中!");
    }
    //电脑重启
    public void reset() {
       state = 2; //配置文件-> 0:恢复正常 2:电脑冒烟 1:电脑蓝屏(陷入蓝屏重启循环,递归崩溃,不建议尝试)
       System.out.println("已重启");
    }
}

Class User

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package Demo;
public class User {
    private Computer computer;
    public User(Computer com) {
       this.computer = com;
    }
    
    public void prelect() throws  NoPlanException {
       try {
           computer.run();
       } catch (LanPingException e) {
           System.out.println(e.getMessage());
           computer.reset();
           //重启后尝试开机
           prelect();
       } catch (MaoYanException e) {
           System.out.println(e.getMessage());
           test();
           throw new NoPlanException("尊敬的领导,电脑坏了,我要下班");
       }
    }
    private void test() {
       System.out.println("电脑冒烟,上自习");
       
    }
}

**Main **Exception_Test

1
2
3
4
5
6
7
8
9
10
11
12
package Demo;
public class Exception_Test {
    public static void main(String[] args) {
       User user = new User(new  Computer());
       try {
           user.prelect();
       } catch (NoPlanException e) {
           System.err.println(e.getMessage());
           System.out.println("换台电脑继续上课");
       }
    }
}
CATALOG