1. 程式人生 > >異常(4)----通過throw、throws關鍵字寫帶有異常的方法

異常(4)----通過throw、throws關鍵字寫帶有異常的方法

二、throws關鍵字

三、自己寫方法丟擲異常

四、舉例

一、throw關鍵字

關鍵字throw,用於在方法的內部丟擲異常。

throw丟擲的是異常的物件。throw後面,必須寫 new 物件(Exception或Exception的子類物件)。

二、throws關鍵字

throws關鍵字標明此方法可能會出現哪些異常,請呼叫者處理。

throws關鍵字是方法中宣告異常的關鍵字。用於在方法的宣告上,標明此方法可能會出現哪些異常,請呼叫者處理。

三、自己寫方法丟擲異常

我們可以自己編寫方法往外拋異常。 規則和原則如下:

1. 當方法內可能丟擲編譯異常時(可以是我們使用throw手動丟擲,也可以是JVM丟擲),必須要寫throws宣告。【例1】

2. 如果方法內可能丟擲編譯異常,但是宣告上沒有寫throws,則程式報錯。【例2】【例3】

3. 如果方法內丟擲了執行異常(可以是我們使用throw手動丟擲,也可以是JVM丟擲),不需要寫throws宣告。(在API文件中,帶有throws的方法,丟擲的異常都是編譯異常。所以自己寫方法的時候也儘量滿足:編譯異常寫throws,執行異常不寫throws)。

例1:

package cn.itcast.demo02;

public class Test {
    public static void main(String[] args) {

    }
    
    //fun():返回最後一個元素*2
    public static int fun(int[] arr) throws Exception{               //throws標明可能丟擲哪些異常
        if(arr == null){
            throw new Exception("傳入的陣列不存在");             //throw往外拋異常,拋給呼叫者


        }
        if(arr.length == 0){    
            throw new Exception("傳入的陣列沒有元素");
        }   
        int i = arr[arr.length -1];
        return i*2;
    }   
}

例2:

package cn.itcast.demo05;


import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    public static void main(String[] args) {
        fun();
    }
    public static void fun(){                              //程式可能丟擲編譯異常,沒有寫throws,報錯。
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date  = sdf.parse("2088-8-8");
        System.out.println(date);
    }
}

執行結果:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type ParseException

    at cn.itcast.demo05.Test.fun(Test.java:13)
    at cn.itcast.demo05.Test.main(Test.java:9)

例3:用throw丟擲了編譯異常,但是沒有使用throws,則程式報錯。

package cn.itcast.demo03;

public class Test {
    public static void main(String[] args) {
        int[] arr = null;
        fun(arr);
    }
    
    public static int fun(int[] arr) {                           //沒有寫throws關鍵字,程式報錯。
        if(arr == null){
            throw new Exception("陣列不存在");
        }
        return arr[3];
    }
}

執行結果:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type Exception

    at cn.itcast.demo03.Test.fun(Test.java:11)
    at cn.itcast.demo03.Test.main(Test.java:6)

四、舉例

編寫一個方法,輸入圓的半徑r,輸出圓的面積。

分析:當輸入的是負數的時候,是異常。雖然能計算Pai*r*r,但是引數違反了真實情況,後面沒有什麼意義了,應該停止程式,不要計算了。所以應該定義一個執行時異常。這個地方丟擲編譯異常不合適,因為就不能讓呼叫者處理。

呼叫者呼叫我們定義的函式fun()的時候,並不知道里面會丟擲執行時異常,也就不會做任何異常方面的處理。當呼叫了fun(-3)時,程式停了,報了"半徑為負"的錯誤,才發現:"奧,原來是我傳了個-3,回去改改"。

package cn.itcast.demo05;

public class Test {
    public static void main(String[] args) {
        double area = fun(-3);
        System.out.println(area);
    }
    public static double fun(double r){
        if(r<0){
            throw new RuntimeException("半徑為負");
        }
        return r*r*Math.PI;
        
    }
}

執行結果:

Exception in thread "main" java.lang.RuntimeException: 半徑為負
    at cn.itcast.demo05.Test.fun(Test.java:14)
    at cn.itcast.demo05.Test.main(Test.java:9)