Loading...

[java]例外迷子

アプリケーションが複雑になるにつれて、バグがでた場所を発見するのが難しくなります。

アプリケーション内部で問題が発生した場合に、まずログ出力を調べるのが通常の対応でしょう。





javaの例外を出力に慣れておくために、サンプルコードで出力されるエラーをみてみます。



例外出力



MyRuntimeExcptions$ARuntimeExcption: MyRuntimeExcptions$BRuntimeExcption: MyRuntimeExcptions$CRuntimeExcption: java.io.FileNotFoundException: @_@!! (No such file or directory)

at TestRuntimeExcption.wrappingExcptionA(TestRuntimeExcption.java:21)

at TestRuntimeExcption.main(TestRuntimeExcption.java:10)

Caused by: MyRuntimeExcptions$BRuntimeExcption: MyRuntimeExcptions$CRuntimeExcption: java.io.FileNotFoundException: @_@!! (No such file or directory)

at TestRuntimeExcption.wrappingExcptionB(TestRuntimeExcption.java:29)

at TestRuntimeExcption.wrappingExcptionA(TestRuntimeExcption.java:19)

... 1 more

Caused by: MyRuntimeExcptions$CRuntimeExcption: java.io.FileNotFoundException: @_@!! (No such file or directory)

at TestRuntimeExcption.wrappingExcptionC(TestRuntimeExcption.java:38)

at TestRuntimeExcption.wrappingExcptionB(TestRuntimeExcption.java:27)

... 2 more

Caused by: java.io.FileNotFoundException: @_@!! (No such file or directory)

at java.io.FileInputStream.open(Native Method)

at java.io.FileInputStream.(FileInputStream.java:106)

at java.io.FileInputStream.(FileInputStream.java:66)

at TestRuntimeExcption.causeFileNotFoundExcption(TestRuntimeExcption.java:77)

at TestRuntimeExcption.causeExcption(TestRuntimeExcption.java:72)

at TestRuntimeExcption.test7(TestRuntimeExcption.java:68)

at TestRuntimeExcption.test6(TestRuntimeExcption.java:64)

at TestRuntimeExcption.test5(TestRuntimeExcption.java:60)

at TestRuntimeExcption.test4(TestRuntimeExcption.java:56)

at TestRuntimeExcption.test3(TestRuntimeExcption.java:52)

at TestRuntimeExcption.test2(TestRuntimeExcption.java:48)

at TestRuntimeExcption.test1(TestRuntimeExcption.java:44)

at TestRuntimeExcption.wrappingExcptionC(TestRuntimeExcption.java:36)

... 3 more





caused byと出力されているところが、例外を発生させた場所ですね。







使用したソースコードです。



import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;



public class TestRuntimeExcption {



public static void main(String[] args) {



try {

wrappingExcptionA();

} catch (Exception e) {

e.printStackTrace();

}



}



public static void wrappingExcptionA() {

try {

wrappingExcptionB();

} catch (Exception e) {

throw new MyRuntimeExcptions.ARuntimeExcption(e);

}

}



public static void wrappingExcptionB() {

try {

wrappingExcptionC();

} catch (Exception e) {

throw new MyRuntimeExcptions.BRuntimeExcption(e);

}

}



public static void wrappingExcptionC() {



try {

test1();

} catch (Exception e) {

throw new MyRuntimeExcptions.CRuntimeExcption(e);

}



}



public static void test1() throws Exception {

test2();

}



public static void test2() throws Exception {

test3();

}



public static void test3() throws Exception {

test4();

}



public static void test4() throws Exception {

test5();

}



public static void test5() throws Exception {

test6();

}



public static void test6() throws Exception {

test7();

}



public static void test7() throws Exception {

causeExcption();

}



public static void causeExcption() throws Exception {

causeFileNotFoundExcption();

}



public static void causeFileNotFoundExcption()

throws FileNotFoundException, IOException {

new FileInputStream("@_@!!").close();

}



public static String causeNullPointerException() {

String s = null;

return s.toString();

}



}









public class MyRuntimeExcptions {



public static class ARuntimeExcption extends RuntimeException {



/**

*

*/

private static final long serialVersionUID = 1L;



public ARuntimeExcption() {

super();

}



public ARuntimeExcption(String message, Throwable cause) {

super(message, cause);

}



public ARuntimeExcption(String message) {

super(message);

}



public ARuntimeExcption(Throwable cause) {

super(cause);

}

}



public static class BRuntimeExcption extends RuntimeException {



/**

*

*/

private static final long serialVersionUID = 1L;



public BRuntimeExcption() {

super();

}



public BRuntimeExcption(String message, Throwable cause) {

super(message, cause);

}



public BRuntimeExcption(String message) {

super(message);

}



public BRuntimeExcption(Throwable cause) {

super(cause);

}



}



public static class CRuntimeExcption extends RuntimeException {



/**

*

*/

private static final long serialVersionUID = 1L;



public CRuntimeExcption() {

super();

}



public CRuntimeExcption(String message, Throwable cause) {

super(message, cause);

}



public CRuntimeExcption(String message) {

super(message);

}



public CRuntimeExcption(Throwable cause) {

super(cause);

}



}

}



リアクション: 

コメントを投稿

ホーム item

このブログを検索

Random Posts

Popular Posts

Labels

ADS