第七章 异常

1、简述Java Error类与Exception类的区别。
Java Error类:所有错误类的祖先类
Exception类:所有异常类的祖先类
错误和异常的区别是,Error不是程序需要捕获和进行处理的,例如OutOfMemmoryError(当Java虚拟机在为对象分配内存空间时,剩余的空间不够,同时也没有可以释放的内容时将会发生这样的错误)不由程序进行捕获或处理,当Error发生时,程序将会停止。

2、简述异常处理的两种方式,并举例说明区别。
在这里插入图片描述
例7.2(隐式声明处理)

1 import java.util.*;
 2 class TestEmptyStack {
 3  public static void main(String[] args){
 4    Stack st = new Stack();
 5    Object ob = st.pop();  
 6  }
 7 
 8 /*
 9 Exception in thread "main" java.util.EmptyStackException
10 at java.util.Stack.peek(Unknown Source)
11 at java.util.Stack.pop(Unknown Source)
12 at TestEmptyStack.main(TestEmptyStack.java:5)
13 */

例7.5(显示声明处理)

1 import java.io. *;
 2 class TestScreenIn{
 3     public static void main(String[] args) throws IOException{   //抛出IOException异常
 4         BufferedReader keyin = new BufferReader(new
 5                                             InputStreamReader(System.in));
 6     String c1;
 7     int i=0;
 8     String[] e = new String[10];
 9     while( i<10){
10         c1 = keyin.readLine();
11         e[i] = c1;
12         i++;
13     }
14    }
15 }

例7.6(异常捕获处理)

 1 public static void main(String[] args){
 2     
 3     try{
 4             .
 5             .
 6             .
 7             c1 = keyin.readLine();
 8             .
 9             .
10             .
11       }
12     catch(IOException e){
13         //e.printStackTrace();
14         System.out.println("系统IO有错误");
15     }
16 }

3、选取RuntimeException类的五个子类,编写抛出并捕获上述子类异常的程序。(例如算术异常, 空指针异常, 类转换异常,数组越界异常)

 1 package TestException;
 2 
 3 class Father{
 4     int i;
 5 }
 6 
 7 class Son extends Father{
 8      public Son(int i){
 9          this.i=i;
10      }
11      public int getI(){
12          return this.i;
13     }
14 }
15 
16 public class CatchException {
17     public static void main(String[] args) {
18         int a=7;
19         int b=0;
20         int c;
21         Father father1 = new Father();
22         Son son1 ;
23         Son son2 = null;
24         int[] x = new int[1];
25 
26         try{
27             c=a/b;
28         }
29 
30         catch (ArithmeticException e){
31             e.printStackTrace();
32             System.out.println("这里发生了算术异常了!!!");
33         }
34         try{
35             son1= (Son) father1;
36         }
37 
38         catch (ClassCastException e){
39             e.printStackTrace();
40             System.out.println("这里发生了类转换异常!!!");
41         }
42 
43         try{
44             son2.getI();
45         }
46 
47         catch (NullPointerException e){
48             e.printStackTrace();
49             System.out.println("这里发生了空指针异常!!!");
50         }
51 
52         try{
53             System.out.println(x[2]);
54         }
55 
56         catch (ArrayIndexOutOfBoundsException e){
57             e.printStackTrace();
58             System.out.println("这里发生了数组越界异常!!!");
59         }
60 
61     }
62 }

4 仿造例7.9,自定义一个异常类,并在某场景下抛出该异常对象

1 package TestException;
 2 
 3 import java.util.Scanner;
 4 
 5 public class MakeException extends Exception{
 6     MakeException(String exa){
 7         super(exa);
 8     }
 9     static void FailExam(int score) throws MakeException{
10         if(score < 60){
11             throw new MakeException("可恶,小伙子你挂科了你知道不?");
12         }
13     }
14     public static void main(String[] args){
15         Scanner input = new Scanner(System.in);//创建一个键盘扫描类对象
16         System.out.println("孩子,大声说出你考了多少分吧");
17         int score = input.nextInt();//输入整型
18         try {
19             FailExam(score);
20             System.out.println("不错,很有精神!");
21         }
22         catch (MakeException e) {
23             e.printStackTrace();
24         }
25     }
26 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值