LoginSignup
2
0

More than 1 year has passed since last update.

例外処理を伝播させる方法を実装しました。(あくまで実験です)

class NyException extends RuntimeException{}

class A{
    void bar() {
        try {
            //クラスをまたがって例外処理を伝播させる。三項演算子で判定している
            throw (1 + Math.random()) * 100 > 0 ? new NyException():new RuntimeException();
        }catch (NullPointerException ex) {
            System.out.println("A ");
        }
    }
}

public class Test {
    public static void main(String[] args) {
        try {
            new A().bar();
        } catch (NyException ex) {
            System.out.println("C ");
        }
    }
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0