ホームページ  >  記事  >  Java  >  Java の例 - カスタム例外

Java の例 - カスタム例外

黄舟
黄舟オリジナル
2017-02-04 10:22:331431ブラウズ

次の例は、Exception を継承することによるカスタム例外の実装を示しています:

/*
 author by w3cschool.cc
 TestInput.java
 */

class WrongInputException extends Exception {
   WrongInputException(String s) {
      super(s);
   }
}
class Input {
   void method() throws WrongInputException {
      throw new WrongInputException("Wrong input");
   }
}
class TestInput {
   public static void main(String[] args){
      try {
         new Input().method();
      }
     catch(WrongInputException wie) {
         System.out.println(wie.getMessage());
      }
   } 
}

上記のコードを実行した出力結果は次のとおりです:

Wrong input

上記は Java の例です - カスタム例外の内容 さらに関連する内容については、こちらをご覧ください。 PHP 中国語 Web サイト (www .php.cn) に注意してください。


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。