JAVA異常設計
JAVAは他のプログラミング言語と同様にすべてを実現することは不可能であり、開発中に異常クラスを自分で拡張して開発ニーズを満たす必要がある場合がある.以下は拡張異常クラスの主な流れとソースコードである.
1、異常類の作成
2、呼び出し及びテスト異常類
1、異常類の作成
package com.util;
/**
*
*/
public class UserdefinedException extends Exception {
private static final long serialVersionUID = 1L;
private String mess;
public UserdefinedException() {
}
public UserdefinedException(String mess) {
this.mess = mess;
}
public String getMessage() {
return mess;
}
}
2、呼び出し及びテスト異常類
package com.biiway.nhwm.plugins.test;
import com.util;
public class TestThrow {
private static final int MAX_PRE_TRANSFER_COIN = 500;
public static void transferCoin(int coin) throws UserdefinedException {
if (coin > MAX_PRE_TRANSFER_COIN) {
throw new UserdefinedException (String.valueOf(coin));
}
}
public static void main(String[] args) {
try {
transferCoin(1000);
} catch (UserdefinedException e) {
System.out.println(e.getMessage());
}
}
}