我应该使用异常来在java中模拟一个goto语句 [英] should I use Exception to simulate a goto statement in java
问题描述
我了解到异常缓慢:
但这篇文章( http://blogs.atlassian.com/2011/05/if_you_use_exceptions_for_path_control_dont_fcept_in_the_stac/ )说我们可以使用异常来模拟一个goto语句:
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();
}
}
现在我很困惑一方面,我希望我的代码干净清晰(像'MyService'类)。另一方面,异常真的很慢。
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.
我应该使用异常来模拟一个goto语句吗?谢谢。
Should I use Exception to simulate a goto statement? Thanks.
推荐答案
不要在正常的程序流中使用异常。它们是为了超出开发者控制范围的特殊情况。它们的速度很慢,效率低下,而且设计用于错误处理,而不是业务逻辑。
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.
刺激goto是当今开发环境中的糟糕的设计决策。他们感到困惑,难以维持。重新构建您的代码以使用中断或其他控制逻辑。
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.
这篇关于我应该使用异常来在java中模拟一个goto语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!