Java面向對象程序設計第7章1-8

Java面向對象程序設計第7章1-8

1.「程序中凡是可能出現異常的地方必須進行捕獲或拋出」,這句話對嗎?

不對。java

異常分兩類,runtime異常和非runtime異常。數組

  1. runtime異常,好比NullPointException等,這一類你不在程序裏面進行try/catch,編譯不會出錯。
  2. 非runtime異常,好比SqlException等或自定義的exception,這一類在程序裏不進行try/catch或throws,編譯就會出錯。

2.自定義一個異常類,並在程序中主動產生這個異常類對象。

class SelfGenerateException extends Exception {
    SelfGenerateException(String msg) {
        super(msg);   //調用Exception的構造方法
    }

    static void throwOne() throws SelfGenerateException {
        int a = 1;
        if (a == 1) {
            //若是a爲1就認爲在特定應用下存在異常,改變執行路徑,拋出異常
            throw new SelfGenerateException("a爲1");
        }
    }

    public static void main(String args[]) {
        try {
            throwOne();
        } catch (SelfGenerateException e) {
            e.printStackTrace();
        }
    }
}

結果:設計

SelfGenerateException: a爲1
    at SelfGenerateException.throwOne(test.java:10)
    at SelfGenerateException.main(test.java:16)

3.藉助JDK幫助,請列舉發生NullPointerException異常的一些狀況。

JDK解釋以下:指針

1571039001833

當應用程序試圖在須要對象的地方使用 null 時,拋出該異常。這種狀況包括:
調用 null 對象的實例方法。
訪問或修改 null 對象的字段。
將 null 做爲一個數組,得到其長度。
將 null 做爲一個數組,訪問或修改其時間片。
將 null 做爲 Throwable 值拋出。
應用程序應該拋出該類的實例來指示其餘對 null 對象的非法使用。code


4.不執行程序,指出下面程序的輸出結果;若是將黑體代碼去掉,寫出輸出結果;若是再將斜體代碼去掉,寫出輸出結果。

public class test
{
    public static void aMethod() throws Exception{
        try{
            throw new Exception();
        }
//------------------黑體------------------------------------------------
        catch(Exception e){
            System.out.println("exception000");
        }
//----------------------------------------------------------------------
//--------------------斜體-----------------------------------------------
        finally{
            System.out.println("exception111");
        }
//---------------------------------------------------------------------------
    }
    public static void main(String[] args){
        try{
            aMethod();
        }
        catch(Exception e){
            System.out.println("exception");
        }
        System.out.println("finished");
    }
}

輸出:
exception000
exception111
finished對象


去黑體輸出:
exception111
exception
finished資源


若是再將斜體代碼去掉:it

Error:(4, 9) java: 'try' 不帶有 'catch', 'finally' 或資源聲明io

就是說try不能單獨存在而不帶有catch或者finally聲明編譯


再去掉aMthod裏的try後

輸出:

exception
finished


5.不執行程序,指出下面程序的輸出結果。

public class test{
    public static String output ="";
    public static void foo(int i){
        try{
            if(i==1) {throw new Exception();}
            output += "1";
        }
        catch(Exception e){
            output += "2";
            return;
        }
        finally{output += "3";}
        output += "4";
    }
    public static void main(String args[]){
        foo(0);
        foo(1);
        System.out.println(test.output);
    }
}

結果:13423

解釋:134是foo(0)輸出的,23是foo(1)輸出的。foo(1)catch裏return後,也會執行finally,可是finally以後的東西就不在執行了。foo(0)不會執行return語句,因此能夠執行finally後面的語句


6. 編寫一個程序方法,對空指針異常、除數爲零異常給出出錯的中文提示。當有新異常發生時,可擴展該方法中的代碼進行統一處理。

public class test {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.charAt(0));//空指針異常
            //System.out.println(1/0);//除零異常
        } catch (NullPointerException e) {
            System.out.println("空指針異常");
        } catch (ArithmeticException e) {
            System.out.println("計算異常");
        } catch (Exception e) {
            System.out.println("其餘異常");
            e.printStackTrace();
        }
    }
}

7.從屏幕輸入10個數,在輸入錯誤的狀況下,給出相應的提示,並繼續輸人。在輸入完成的狀況下,找到最大最小數。

import java.util.Arrays;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[10];
        int index = 0;
        while (true) {
            try {
                arr[index] = sc.nextInt();
                if(index!=9)
                    index++;
                else{
                        break;
                }
            } catch (Exception e) {
                sc.next();
                System.out.println("輸入類型有誤,請從新輸入");
            }
        }
        Arrays.sort(arr);
        System.out.println("max=" + arr[index]);
        System.out.println("min=" + arr[0]);
    }
}

8.閱讀下面程序,TimedOutException爲自定義異常,完成指定方法後面的部分。

public void method()throws TimedOutException
          {
              success= connect();
              if(success == -1)
              {
                  throw new TimedOutException();
              }
          }
相關文章
相關標籤/搜索