Javaで開発するためにIDE Netbeans7.3を使用しています。自分では説明できない不思議なことがあるので、どうか理解を助けてください。
私はクラスに宣言しました。最初のものは から継承しException
ます:
public class MyParameterException extends Exception{
public MyParameterException(){
super();
}
public MyParameterException(String message){
super(message);
}
}
2 つ目は NullPointerException から継承します。
public class NullMyParameterException extends NullPointerException{
public NullMyParameterException(){
super();
}
public NullMyParameterException(String message){
super(message);
}
}
さて、クラスでメソッドを作成し、次のように記述します。
public void test(String s){
if(s==null) throw new NullMyParameterException("The input string is null.");
if(s.trim().isEmpty()) throw new MyParameterException("The input string is empty.");
}
奇妙に思えるのはunreported exception MyParameterException must be caught or declared to be thrown
、IDE からメッセージを受け取ることですが、メソッドでスローできる最初の例外については何も言われていません。
私の知る限り、メソッドは次のように宣言されると予想されます。
public void test(String str) throws MyNullParameterException, MyParameterException
ただし、Netbeans のみで十分です。
public void test(String str) throws MyParameterException
これは:
- IDE のバグ。
NullPointerException
から継承するクラスは特殊なので正常です。- ...
どうか、私に理解させてください。