아래는 간단하게 자신만의 Exception 을 만드는 방법이다. 어디서 참고했는지는 기억이 나지 않아서 적지 못하겠다.
아래처럼 Exception 을 하나 정의하고, 그 안에 static class 로 exception 들을 추가로 정의하면, 일괄적으로 관리할 수 있어서 편한 듯 하다.
/**
* Created by namh on 14. 2. 15.
*/
public abstract class BaseException extends Exception{
// Each exception message will be hold here
private String message;
public BaseException(String msg)
{
this.message = msg;
}
public String getMessage() {
return message;
}
}
/**
* Created by namh on 14. 2. 15.
*/
public class NewMyException {
public static class BadParameter extends BaseException
{
private static final long serialVersionUID = 3555714415375055302L;
public BadParameter(String msg) {
super(msg);
}
}
}
사용할 때는 아래처럼 하면 된다.
throw new NewMyException.BadParameter("msg")
댓글 없음:
댓글 쓰기