<< 例外をキャッチする(try, catch, finally) | main | 例外オブジェクト(Exception) >>

スポンサーサイト

  • 2013.11.08 Friday
  • -
  • -
  • -
  • by スポンサードリンク

一定期間更新がないため広告を表示しています


例外を投げる(throw, throws)

throw を用いて例外を投げることができます。例外を投げるメソッドを定義する場合は、throws を用いて、そのメソッドが例外を投げる可能性があることを明示しておきます。throws 宣言されたメソッドは、try catch で例外を補足しないと、コンパイル時にエラーとなります。

§ThrowTest.java
class MyException extends Exception {
public String errMsg;
MyException(String msg) { errMsg = msg; }
}

class ThrowTest {
public static void main(String[] args) {
try {
methodA();
} catch (MyException e) {
System.out.println(e.errMsg);
}
}
static void methodA() throws MyException {
MyException e = new MyException("エラー発生!!");
throw e;
}
}


スポンサーサイト

  • 2013.11.08 Friday
  • -
  • 21:39
  • -
  • -
  • by スポンサードリンク

コメント
コメントする









この記事のトラックバックURL
トラックバック
相互リンク
selected entries
categories
archives
profile
search this site.
others
mobile
qrcode
powered
無料ブログ作成サービス JUGEM
calendar
     12
3456789
10111213141516
17181920212223
24252627282930
31      
<< March 2024 >>
sponsored links