Java自定義異常的建立及多層呼叫

SaltedFish00發表於2020-12-16

步驟:
1.建立異常類,繼承Exception類;
2.在類中建立兩個構造方法,一個帶報錯資訊引數,一個不帶;
3.帶報錯資訊引數的構造方法要繼承父類(Exception類)的構造方法,目的是 e.getMessage()和報異常時能獲取報錯資訊
4.最後一層呼叫catch異常,之前的只要丟擲異常
5.呼叫丟擲異常的方法,必須throws該異常
6.第一次遇到判斷異常的方法A,要new一個異常,才能丟擲;繼而,呼叫A的其他方法也有異常可以丟擲

帶報錯資訊引數的構造方法繼承父類的構造方法:
在這裡插入圖片描述

不繼承父類方法:
在這裡插入圖片描述

程式碼:
建立了IndexIsNagetiveException和IndexIsOutofRangeException兩個自定義異常,用於insert時丟擲負數位置和超界位置,並在列印異常時進行詳細說明

package test;

public class MyStringBuffer implements IStringBuffer{
    //自定義丟擲異常
    public class IndexIsNagetiveException extends Exception {
        public IndexIsNagetiveException() {
        }

        public IndexIsNagetiveException(String message) {
            super(message); //使e.getMessage()能獲取message
        }
    }
    public class IndexIsOutofRangeException extends Exception {
        public IndexIsOutofRangeException() {
        }

        public IndexIsOutofRangeException(String message) {
            super(message);
        }
    }
    int capacity = 16;
    int length = 0;
    char[] value;
    public MyStringBuffer(){
        value = new char[capacity];
    }

    //有參構造方法
    public MyStringBuffer(String str){
        this();
        if(null==str)
            return;

        if(capacity<str.length()){
            capacity  = value.length*2;
            value=new char[capacity];
        }

        if(capacity>=str.length())
            System.arraycopy(str.toCharArray(), 0, value, 0, str.length());

        length = str.length();

    }

    @Override
    public void append(String str) throws IndexIsNagetiveException, IndexIsOutofRangeException  {

        insert(length,str);
    }

    @Override
    public void append(char c) throws IndexIsNagetiveException, IndexIsOutofRangeException {
        append(String.valueOf(c));

    }

    @Override
    public void insert(int pos, char b) throws IndexIsNagetiveException, IndexIsOutofRangeException {

        insert(pos, String.valueOf(b));

    }

    @Override
    public void delete(int start) {

        delete(start,length);
    }

    @Override
    public void delete(int start, int end) {
        //邊界條件判斷
        if(start<0)
            return;

        if(start>length)
            return;

        if(end<0)
            return;

        if(end>length)
            return;

        if(start>=end)
            return;

        System.arraycopy(value, end, value, start, length- end);
        length-=end-start;

    }

    @Override
    public void reverse() {

        for (int i = 0; i < length/2; i++) {

            char temp = value[i];
            value[i] = value[length-i-1];
            value[length-i-1] = temp;
        }

    }

    @Override
    public int length() {
        // TODO Auto-generated method stub
        return length;
    }


    @Override
    public void insert(int pos, String b) throws IndexIsNagetiveException, IndexIsOutofRangeException {

        //邊界條件判斷
        if(pos<0){
            throw new IndexIsNagetiveException("插入的位置是"+pos+",為負");
        }

        if(pos>length){
            throw new IndexIsOutofRangeException("插入的位置是"+pos+",超越最大界:"+length);
        }

        if(null==b){
            return;
        }


        //擴容
        while(length+b.length()>capacity){
            capacity = (int) ((length+b.length())*1.5f);
            char[] newValue = new char[capacity];
            System.arraycopy(value, 0, newValue, 0, length);
            value = newValue;
        }

        char[] cs = b.toCharArray();

        //先把已經存在的資料往後移

        System.arraycopy(value, pos, value,pos+ cs.length, length-pos);
        //把要插入的資料插入到指定位置
        System.arraycopy(cs, 0, value, pos, cs.length);

        length = length+cs.length;

    }

    public String toString(){

        char[] realValue = new char[length];

        System.arraycopy(value, 0, realValue, 0, length);

        return new String(realValue);

    }

    public static void main(String[] args)  {
        MyStringBuffer sb = new MyStringBuffer("there light");
        System.out.println(sb);
        //丟擲異常
        try{
            sb.insert(-1, "let ");
        } catch (IndexIsNagetiveException | IndexIsOutofRangeException e) {
            System.out.println("具體原因:"+e.getMessage());
            e.printStackTrace();
        }
    }

}

相關文章