简体   繁体   English

If-Else语句与Exception-Catcher?

[英]If-Else statements vs Exception-Catcher?

I got tired of adding seemingly endless if-else statements into my code, so I wondered if it would be better practice if I'd just catch Exceptions if something wasn't right. 我已经厌倦了在代码中添加看似无尽的if-else语句,所以我想知道,如果有什么不对的地方,我只捕获Exceptions是否是更好的做法? Eg. 例如。 instead of saying: 而不是说:

public static boolean firstStringOfArrayIsTheSameAsTheWord(String word, String[] array) {
    if(word != null) {
        if(array.length > 0) {
            return word.equalsIgnoreCase(array[0]);
        }
    }
    return false;
}

I'd just have: 我只有:

public static boolean firstStringOfArrayIsTheSameAsTheWord(String word, String[] array) {
    try {
        return word.equals(array[0]);
    } catch(/*NullPointerException or an ArrayIndexOutOfBoundsException*/ Exception e) {
        return false;
    }
}

Note that I do know that I could use multiple arguments in the if-statements, but I'm just interested in what method would be better-to-use in practice and why. 请注意,我确实知道可以在if语句中使用多个参数,但是我只是对哪种方法在实践中会更好地使用以及为什么感兴趣感兴趣。

Thanks in advance! 提前致谢!

No, thats not a good idea, thats an abuse of Exception handling. 不,那不是一个好主意,那是对异常处理的滥用。

You are meant to avoid unnecesary exception catching, exceptions should only be used for things that go wrong because they are outside of your control and not as part of the normal program flow. 您应避免不必要的异常捕获,仅应将发生异常的情况用于异常,因为异常不在您的控制范围之内,而不是正常程序流程的一部分。 Also, as @SJuan76 said you'll be hiding situations where there is a real Exception. 此外,正如@ SJuan76所说,您将隐藏存在真正异常的情况。

If you are tired of using if-else statements, you can try using a switch (which will even work for strings in Java 7) or improve polymorphism in your application. 如果您厌倦了使用if-else语句,则可以尝试使用一个switch (该switch甚至适用于Java 7中的字符串 )或改善应用程序中的多态性

The general rule is, "use exceptions for exceptional events, never for control flow". 一般规则是:“对异常事件使用例外,对控制流绝不使用”。

So use the if(...) else ... please. 因此,请使用if(...) else ...

First, IIRC correctly exception handling is slow. 首先,正确地IIRC异常处理很慢。 Not terribly slow, but nothing that you want to use in the mainstream logic. 并不是很慢,但是您不想在主流逻辑中使用任何东西。

Second, by working that way, you will be hiding the situations where there is a real Exception. 其次,通过这种方式,您将隐藏存在真正异常的情况。 Your user will try to load a file, but will only find that the file was not loaded, would not get any idea if the file was not found, the data was corrupt, whatever. 您的用户将尝试加载文件,但只会发现该文件未加载,如果找不到该文件,数据已损坏等,则不会有任何想法。 You lose a lot of information. 您会丢失很多信息。

If you want to make your code simpler, it would be better to do something like 如果您想简化代码,最好执行以下操作

/**
 * ....
 * @param word String must not be null.
 * @param array String[] must not be null, have length at least 1, an array[0] must not be null
 */
public static boolean firstStringOfArrayIsTheSameAsTheWord(String word, String[] array) {
  return word.equals(array[0]);
}

At least you delegate the responsability for sanitizing the parameters to the code that uses your method. 至少您将负责清理参数的责任委托给使用您的方法的代码。 Not a sensible thing to do (it goes against defensive programming) and your coworker will hate working with your code, but better than your first approach. 这不是明智的选择(这与防御性编程相违背),您的同事会讨厌使用您的代码,但是比您的第一种方法更好。

do {
    if (conditionA) {
        something;
    }
    else {
        break;
    }
    if (conditionB) {
        somethingElse;
    }
    else {
        break;
    }
    ...
} while(false);

The exception approach is not a good idea. 异常方法不是一个好主意。 Throwing an exception 1. it is slow 2. makes your application unreadable. 引发异常1.速度慢2.使您的应用程序不可读。 As @greedybuddha said "use exceptions for exceptional events, never for control flow". 正如@greedybuddha所说:“对异常事件使用例外,对控制流绝不使用”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM