0

提供されたパラメーターに基づいて例外をスローする別のクラスの別の関数を呼び出す関数があります。私が欲しい

public class A {
    public int f(int p){
    {
         B obj = new B();
         obj.g(p);
    }
}

public class B {
    public int g(int p)
    {
        // throws an exception for this value of p
    }
}

class Aそれ自体で例外をキャッチして処理することは可能ですか? の実装を変更することはできませんclass B

4

1 に答える 1

1

そう、try-catch ステートメントを使用するだけです。

public class A {
    public int f(int p){
    {
         B obj = new B();
         try {
             obj.g(p);
         } catch ( /* the exception */ ) {
             // handle the exception
         }
    }
}

public class B {
    public int g(int p)
    {
        // throws an exception for this value of p
    }
}
于 2013-09-12T17:48:07.503 に答える