java 抛出异常与使用 switch 语句返回空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2567932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:50:01  来源:igfitidea点击:

Throwing exception vs returning null value with switch statement

javareturn-value

提问by Greg

So I have function that formats a date to coerce to given enum DateType{CURRENT, START, END} what would be the best way to handling return value with cases that use switch statement

所以我有函数格式化日期以强制给定的枚举 DateType{CURRENT, START, END} 在使用 switch 语句的情况下处理返回值的最佳方法是什么

public static String format(Date date, DateType datetype) {
    ..validation checks

    switch(datetype){
    case CURRENT:{
        return getFormattedDate(date, "yyyy-MM-dd hh:mm:ss");
    }               
    ... 
     default:throw new ("Something strange happend");
    }

}

OR throw excpetion at the end

或在最后抛出异常

   public static String format(Date date, DateType datetype) {
            ..validation checks

            switch(datetype){
            case CURRENT:{
                return getFormattedDate(date, "yyyy-MM-dd hh:mm:ss");
            }               
            ... 
            }

               //It will never reach here, just to make compiler happy 
        throw new IllegalArgumentException("Something strange happend");    
        }

OR return null

或返回空

public static String format(Date date, DateType datetype) {
            ..validation checks

            switch(datetype){
            case CURRENT:{
                return getFormattedDate(date, "yyyy-MM-dd hh:mm:ss");
            }               
            ... 
            }

             return null;   
}

What would be the best practice here ? Also all the enum values will be handled in the case statement

这里的最佳做法是什么?此外,所有枚举值都将在 case 语句中处理

回答by Bozho

Throw an exception, since this is an exceptional case.

抛出异常,因为这是一个例外情况。

And throw it outside the switch, it would be more readable. Otherwise it sounds like "the default case is exceptional".

并将其扔到 之外switch,它会更具可读性。否则听起来像“默认情况是例外”。

回答by Hyman

I think that throw new IllegalArgumentException("Something strange happend")is the best pratice.

我认为这throw new IllegalArgumentException("Something strange happend")是最好的做法。

Using nullwill just presumibly cause a NullPointerExceptionsomewhere when you use the return value but it will be less informativethan raising a specific exception that describes the problem!

当您使用返回值时,使用null可能只会在NullPointerException某个地方导致某个地方,但与引发描述问题的特定异常相比,它提供的信息更少

And you know: clear errors = better developing.

您知道:清除错误 = 更好地开发。

回答by Bj?rn Pollex

I would go with the first approach (but with IllegalArgumentExceptionas in your second approach). You should include a default statement to guard against cases when someone modifys (extends) your enum. Putting the exception in the default-statement makes clear to the reader that the code is never supposed to get past the switch-statement. Otherwise they would have to check if really all of the enum values are in the switch.

我会采用第一种方法(但与IllegalArgumentException您的第二种方法一样)。您应该包含一个默认语句,以防止有人修改(扩展)您的枚举的情况。将异常放在 default-statement 中可以让读者清楚地知道代码永远不应该超过 switch-statement。否则他们将不得不检查是否真的所有枚举值都在开关中。

回答by drahnr

Exceptions, as you can obey more to the parent than a single return int can. Usually you use Exceptions where they exist (C++), and return values where not (C).

例外,因为您可以比单个 return int 更能服从父级。通常你在它们存在的地方使用异常(C++),并在不存在的地方返回值(C)。