should I use Exception to simulate a goto statement in java

依然范特西╮ 提交于 2019-12-11 07:17:45

问题


I've learned that Exception is slow:

How slow are Java exceptions?

but this article(http://blogs.atlassian.com/2011/05/if_you_use_exceptions_for_path_control_dont_fill_in_the_stac/) says that we can use Exception to simulate a goto statement:

so I think it's ok to write my code like this:

public class MyService {

    public Result service(int i) {
        Result result = new Result();
        try {

            Util.checkCommonArguments(i);

            //my business logic...
            if ((i % 2) != 0) {
                throw new BizException("002", "can not be odd");
            }
            if (i > 200) {
                throw new BizException("003", "can not be greater than 200");
            }

            // the normal processing...


            result.setCode("000");
            result.setDesc("ok");
        } catch (BizException e) {
            result.setCode(e.getCode());
            result.setDesc(e.getMessage());
        } catch (Exception e) {
            result.setCode("999");
            result.setDesc("system error");
        }
        return result;
    }


}

class Util {
    public static void checkCommonArguments(int input) {
        if (input < 0) {
            throw new BizException("001", "can not be negative.");
        }
        //maybe more
    }
}

class Result {

    private String code;
    private String desc;

    //getter and setter
}

class BizException extends RuntimeException {
    private String code;

    public BizException(String code, String message) {
        super(message);
        this.code = code;
    }
    @Override
    public Throwable fillInStackTrace()
    {
        return this;
    }
}

but 'dont fill in the stack trace' does not work:

// throw but catch, but not Filling in exception stack traces 
public void method5(int i) {
    try {
        value = ((value + i) / i) << 1;
        // i & 1 is equally fast to calculate as i & 0xFFFFFFF; it is both
        // an AND operation between two integers. The size of the number plays
        // no role. AND on 32 BIT always ANDs all 32 bits
        if ((i & 0x1) == 1) {
            throw new MyBizException();
        }
    } catch (MyBizException e) {
        //maybe do something
    }
}

method5's cost time is almost the same as:

    // This one will regularly throw one
public void method3(int i) throws Exception {
    value = ((value + i) / i) << 1;
    // i & 1 is equally fast to calculate as i & 0xFFFFFFF; it is both
    // an AND operation between two integers. The size of the number plays
    // no role. AND on 32 BIT always ANDs all 32 bits
    if ((i & 0x1) == 1) {
        throw new Exception();
    }
}

Now I'm confused. On one side, I want my code clean and clear(like the class 'MyService'). On the other side, Exception is really slow.

Should I use Exception to simulate a goto statement? Thanks.


回答1:


Don't use exceptions for normal program flow. They are for exceptional circumstances beyond the developer's control. They are slow, inefficient, and designed for error handling, not business logic.

Stimulating a goto is a bad design decision in today's development environment anyways. They are confusing to follow, and difficult to maintain. Refactor your code to use breaks or other control logic instead.




回答2:


Using exception for flow control is neither in the interest of good design nor efficient. At the very minimum this will create unnecessary objects. I would encourage you to have a look at Joshua Bloch's "Effective Java" which explicitly covers this topic.



来源:https://stackoverflow.com/questions/29358806/should-i-use-exception-to-simulate-a-goto-statement-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!