一定期間更新がないため広告を表示しています
例外を投げる(throw, throws)
- 2006.08.08 Tuesday
- 例外処理
- 21:39
- comments(0)
- trackbacks(0)
- by javaget
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;
}
}
§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;
}
}
- コメント
- コメントする
- この記事のトラックバックURL
- トラックバック
- selected entries
-
- 例外を投げる(throw, throws) (08/08)
- categories
- archives
-
- August 2006 (88)
- profile
- search this site.
- mobile
- calendar
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
- sponsored links