Tbpgr Blog

Recruiting Operations tbpgr(てぃーびー) のブログ

書籍 Effective Java | 詳細メッセージにエラー記録情報を含める

パンくず

Effective Java
詳細メッセージにエラー記録情報を含める

概要

詳細メッセージにエラー記録情報を含める

内容

例外の詳細メッセージは実際に問題が起こった場合の解析が容易になるように
エラーの原因と関わるパラメータのすべてを出力するべきです。

サンプルコード

package effective.creation.chapter9;

public class SampleNo63 {
    private static final String NABEATSU = "Nabeatsu";
    private static final String OKAMURA = "Okamura";

    public static class IllegalNabeatsuExeption extends Exception {
        public IllegalNabeatsuExeption(int number, String name) {
            super("IllegalNabeatsuExeption [number=" + number + ", name=" + name + "]");
        }
    }

    public static void main(String[] args) {
        for (int i = 1; i < 40 + 1; i++) {
            printAhoStepByThree(i, NABEATSU);
        }
        printAhoStepByThree(0, NABEATSU);
        printAhoStepByThree(3, null);
        printAhoStepByThree(3, "");
        printAhoStepByThree(3, OKAMURA);
    }

    private static void printAhoStepByThree(int number, String name) {
        try {
            System.out.println(getAhoStepByThree(number, name));
        } catch (IllegalNabeatsuExeption e) {
            e.printStackTrace();
        }
    }

    public static String getAhoStepByThree(int number, String name) throws IllegalNabeatsuExeption {
        if (number == 0) {
            throw new IllegalNabeatsuExeption(number, name);
        }
        if (name == null || name.equals("")) {
            throw new IllegalNabeatsuExeption(number, name);
        }
        if (!name.equals(NABEATSU)) {
            throw new IllegalNabeatsuExeption(number, name);
        }

        if (number % 3 == 0) {
            return number + " !!(アホっぽく)";
        } else {
            return number + "";
        }
    }
}

出力

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
30 !!(アホっぽく)
31
32
33 !!(アホっぽく)
34
35
36 !!(アホっぽく)
37
38
39 !!(アホっぽく)
40
effective.creation.chapter9.SampleNo63$IllegalNabeatsuExeption: IllegalNabeatsuExeption [number=0, name=Nabeatsu]
	at effective.creation.chapter9.SampleNo63.getAhoStepByThree(SampleNo63.java:33)
	at effective.creation.chapter9.SampleNo63.printAhoStepByThree(SampleNo63.java:25)
	at effective.creation.chapter9.SampleNo63.main(SampleNo63.java:17)
effective.creation.chapter9.SampleNo63$IllegalNabeatsuExeption: IllegalNabeatsuExeption [number=3, name=null]
	at effective.creation.chapter9.SampleNo63.getAhoStepByThree(SampleNo63.java:36)
	at effective.creation.chapter9.SampleNo63.printAhoStepByThree(SampleNo63.java:25)
	at effective.creation.chapter9.SampleNo63.main(SampleNo63.java:18)
effective.creation.chapter9.SampleNo63$IllegalNabeatsuExeption: IllegalNabeatsuExeption [number=3, name=]
	at effective.creation.chapter9.SampleNo63.getAhoStepByThree(SampleNo63.java:36)
	at effective.creation.chapter9.SampleNo63.printAhoStepByThree(SampleNo63.java:25)
	at effective.creation.chapter9.SampleNo63.main(SampleNo63.java:19)
effective.creation.chapter9.SampleNo63$IllegalNabeatsuExeption: IllegalNabeatsuExeption [number=3, name=Okamura]
	at effective.creation.chapter9.SampleNo63.getAhoStepByThree(SampleNo63.java:39)
	at effective.creation.chapter9.SampleNo63.printAhoStepByThree(SampleNo63.java:25)
	at effective.creation.chapter9.SampleNo63.main(SampleNo63.java:20)