开发者

Substitute for exception causes in Java ME

开发者 https://www.devze.com 2022-12-26 20:21 出处:网络
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 sam

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

精彩评论

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

关注公众号