加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 返回boolean或try catch

发布时间:2020-12-15 08:27:56 所属栏目:Java 来源:网络整理
导读:当函数或方法包含错误/无效数据时,返回false或抛出异常? 考虑一个Loginer类有这样的方法: public boolean login(String username){ //retrieve data... if(username.equals(record.username)){ return true; } return false;} 然后在主要或其他类 String u
当函数或方法包含错误/无效数据时,返回false或抛出异常?
考虑一个Loginer类有这样的方法:

public boolean login(String username){
    //retrieve data...
    if(username.equals(record.username)){
        return true;
    }
    return false;
}

然后在主要或其他类

String username = "ggwp";
if(Loginer.login(username)){
    //successful login,show homepage...
    new User(username);
} else {
    //invalid username
}

它不会效率低,因为它已经使用if-else语句两次检查,一次在Loginer中,另一次在main处再次检查为true.
不会尝试捕获会做同样的事情吗?让Loginer抛出异常:

public User login(String username){
    //retrieve record data...
    if(username.equals(record.username)){
        return new User(username);
    }

    /* Exception if no record found for such username */
    throw new MyException("invalid username");
}

然后在主要:

String username = "ggwp2";
User theUser;
try{
    //sucessful login
    theUser = Loginer.login(username);
}catch(MyException e){
    //invalid username
}

try-catch不需要第二次检查true或false. (这个例子我使用返回User对象,它可能是void并且只返回,但是为什么使用boolean,最终会被检查两次?)

一些网站消息人士说不要使用try-catch进行“代码跳转”,但在这种情况下它只是做同样的事情. (try-catch与if-else语句太相似)

那么哪个是正确的,为什么?如果这个问题不正确,请指导和抱歉,我是新手OO.

解决方法

简短回答:

你永远不应该使用try / catch来实现“控制逻辑”.

正如Andy Turner所说,“只使用异常来处理异常情况.”

所有支持异常的语言都是如此 – 不仅仅是Java.有用的文章:

Best practices for exceptions

PS:try / catch与“if / else”不“相似”.它有不同的实现,不同的意图……而且它的价格更贵.

附加说明:

Exceptions: Why throw early? Why catch late?

07001

In my experience,its best to throw exceptions at the point where the
errors occur. You do this because it’s the point where you know the
most about why the exception was triggered.

As the exception unwinds back up the layers,catching and rethrowing
is a good way to add additional context to the exception. This can
mean throwing a different type of exception,but include the original
exception when you do this.

Eventually the exception will reach a layer where you are able to make
decisions on code flow (e.g a prompt the user for action). This is the
point where you should finally handle the exception and continue
normal execution.

With practice and experience with your code base it becomes quite easy
to judge when to add additional context to errors,and where it’s most
sensible to actually,finally handle the errors.

  • Catch → Rethrow

    Do this where you can usefully add more information that would save
    a developer having to work through all the layers to understand the
    problem.

  • Catch → Handle

    Do this where you can make final decisions on what is an
    appropriate,but different execution flow through the software.

  • Catch → Error Return

    Whilst there are situations where this is appropriate,catching exceptions and returning an error value to the caller should be considered for refactoring into a Catch → Rethrow implementation.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读