开发者

Substitute for exception causes in Java ME

In Java SE it is possi开发者_Go百科ble set the cause of an exception using initCause to avoid losing information about the error when catching and rethrowing an exception. Is it possible to do the same in Java ME?


It is easy to extend the Exception class to achieve this:

public class OperationFailedException extends Exception{
    public Exception cause;
    public OperationFailedException(String string, Exception ex) {
        super(string);
        cause=ex;
    }

    public void printStackTrace(){
        super.printStackTrace();
        System.err.println("\nCaused by:\n");
        cause.printStackTrace();
    }
}

This exception is useful for hiding the underlying exceptions, such as when we want to avoid dependancies or when we want to avoid forcing the caller to deal with too many types of exceptions. I generally also create a version of this class extending RuntimeException for wrapping exception that I want to be unchecked

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜