我应该使用Exception在java中模拟goto语句吗
我了解到异常是缓慢的: 但是本文()说我们可以使用Exception来模拟goto语句: 因此,我认为可以这样编写代码:我应该使用Exception在java中模拟goto语句吗,java,exception,goto,Java,Exception,Goto,我了解到异常是缓慢的: 但是本文()说我们可以使用Exception来模拟goto语句: 因此,我认为可以这样编写代码: public class MyService { public Result service(int i) { Result result = new Result(); try { Util.checkCommonArguments(i); //my business logic
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;
}
}
公共类MyService{
公共成果服务(int i){
结果=新结果();
试一试{
Util.checkCommonArguments(i);
//我的商业逻辑。。。
如果((i%2)!=0){
抛出新BizException(“002”,“不能是奇数”);
}
如果(i>200){
抛出新BizException(“003”,“不能大于200”);
}
//正常处理。。。
结果:设置代码(“000”);
结果:setDesc(“正常”);
}捕获(商业例外){
result.setCode(e.getCode());
result.setDesc(e.getMessage());
}捕获(例外e){
结果。设置代码(“999”);
结果setDesc(“系统错误”);
}
返回结果;
}
}
类Util{
公共静态void checkCommonArguments(int输入){
如果(输入<0){
抛出新的BizException(“001”,“不能为负”);
}
//也许更多
}
}
班级成绩{
私有字符串码;
私有字符串描述;
//接二连三
}
类BizException扩展了RuntimeException{
私有字符串码;
公共业务异常(字符串代码、字符串消息){
超级(信息);
this.code=代码;
}
@凌驾
公共可丢弃的fillInStackTrace()
{
归还这个;
}
}
但是“不填充堆栈跟踪”不起作用:
// 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();
}
}
//抛出但捕获,但不填充异常堆栈跟踪
公开作废方法5(int i){
试一试{
value=((value+i)/i)在正常程序流中不使用异常。它们适用于开发人员无法控制的异常情况。它们速度慢、效率低,设计用于错误处理,而不是业务逻辑
无论如何,在当今的开发环境中,刺激goto是一个糟糕的设计决策。它们令人困惑,难以维护。重构代码以使用中断或其他控制逻辑。不要在正常程序流中使用异常。它们适用于开发人员无法控制的异常情况。它们速度很慢,效率低下,设计用于错误处理,而不是业务逻辑
无论如何,在当今的开发环境中,刺激goto是一个糟糕的设计决策。它们很难遵循,也很难维护。重构代码以使用中断或其他控制逻辑来代替。使用异常进行流控制既不利于良好的设计,也不利于效率。这至少会造成unn必要对象。我鼓励您看看Joshua Bloch的“有效Java”,它明确地涵盖了这一点。使用异常进行流控制既不利于良好的设计,也不利于高效。至少这会创建不必要的对象。我鼓励您看看Joshua Bloch的“有效Java”这明确地涵盖了这一点。请在问题正文中添加您的实际问题。这不是一个坏方法,实际上相当普遍。有两件事需要说明:您没有模拟gotos,那些基本的日子已经过去了。但是您当然会更改执行流程。您一次只能处理一个异常-这可能合适,也可能不合适用于验证。另一种常见的模式是添加到某种验证错误列表中…请在问题正文中添加实际问题。这不是一种坏方法,实际上非常常见。有两件事需要说明:你不模拟gotos,那些基本的日子已经过去了。但当然,你改变了执行流程。你只能处理一次出现一个异常-这可能适用于您的验证,也可能不适用于您的验证。另一种常见模式是添加到某种验证错误列表中…在“异常情况”部分中不出售。在将字符串
转换为整数
之前,您不会手动验证字符串,只是为了避免“缓慢且低效”NumberFormatException
,你呢?;)@jangroth没有,但那是因为没有其他选择。如果Integer.valueOf(string)会更好
返回null
而不是抛出一个NumberFormatException
。在许多语言中,我都会这样做。在.Net中,我会用tryparse,在其他语言中,我会在本地检查它。正如前面提到的,当您没有好的选择时,您可以使用它。但是不管怎样,如果我希望字符串在传递给我时是数字的,那么事实就是它实际上不是例外。预期的数字,还有其他东西。如果我正在为该呼叫编写联系人,我会在其中输入我预期的数字的字符串表示形式。不在“例外情况”部分销售。在将字符串转换为整数
之前,您不会手动验证该字符串,只是为了避免速度慢且效率低“NumberFormatException
,是吗?;)@jangroth没有,但那是因为没有其他选择。如果Integer.valueOf(string)会更好
返回null
而不是抛出一个NumberFormatException
。在许多语言中,我都会这样做。在.Net中,我会用tryparse,在其他语言中,我会在本地检查它。正如前面提到的,当您没有好的选择时,您可以使用它。但是不管怎样,如果我希望字符串在传递给我时是数字的,那么事实就是它实际上不是例外。预期的号码,得到了其他东西。如果我正在为那个电话写联系人,我会在那里输入我预期的一个号码的字符串表示形式。