Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从catch块抛出多个异常的最佳方法_Java_Java 8 - Fatal编程技术网

Java 从catch块抛出多个异常的最佳方法

Java 从catch块抛出多个异常的最佳方法,java,java-8,Java,Java 8,我正在为其中一个项目编写一些异常处理,我必须根据catch块中的错误代码抛出异常。我是通过以下方式完成的: public BankStatistics fetchBankStatistics(final Customer id) { final BankStatistics bs= this.bankStatService.fetchStatDetails(id); return bs; } catch (final StaticticsProcessingexception e

我正在为其中一个项目编写一些异常处理,我必须根据catch块中的错误代码抛出异常。我是通过以下方式完成的:

public BankStatistics fetchBankStatistics(final Customer id) {
    final BankStatistics bs= this.bankStatService.fetchStatDetails(id);
    return bs;
} catch (final StaticticsProcessingexception ex) {
    if (ex.getErrorCode().equals("STAT-102")) {
      // log the error
      throw new MyNewCustomException(ERROR.INVALID_CUST_ID)
    }
    if (ex.getErrorCode().equals("STAT-103")) {
      // log the error
      throw new MyNewCustomException(ERROR.CUST_DOES_NOT_EXIST)
    }
    if (ex.getErrorCode().equals("STAT-104")) {
      // log the error
      throw new MyNewCustomException(ERROR.CUST_NOT_AUTHENTICATED)
    }
    return null;
}

上面的代码可以编译,但我不喜欢从catch块返回null。如果您能给出一些建议,上面的代码是否是处理此场景的最佳方法,或者是否还有其他方法?

为什么不在这种情况下抛出ex?

您可以抛出带有空消息的NewCustomException

throw new MyNewCustomException();

在这种情况下,我提供以下我的观点。我建议将错误代码映射作为键,其他细节作为值。例如,您可以这样维护

Map<String,String> errMap = new HashMap<>();
errMap.put("STAT-102",ERROR.INVALID_CUST_ID);
errMap.put("STAT-103",ERROR.CUST_DOES_NOT_EXIST);
errMap.put("STAT-104",ERROR.CUST_NOT_AUTHENTICATED);
catch (final StaticticsProcessingexception ex) {
      throw new MyNewCustomException(errMap.get(ex.getErrorCode()))
}

正如Kayman Sir所建议的,您还可以抛出IllegalArgumentException,除了基于您的业务需求之外,您还可以创建自己的异常类,并通过处理错误来抛出它。

StaticsProcessingException来自另一个类,我不想向用户公开。所以我有自己的自定义异常类来处理这个问题。@RoyalTiger然后可能抛出一个
IllegalStateException
或类似的东西,因为您的代码不准备处理特定的代码流。然后可能抛出类似MyNewCustomException(ERROR.UNKNOWN\u exception)的东西?是,我当然可以。将
返回null
替换为
抛出新的MyNewCustomException(ERROR.OTHER,ex)
——第二个参数是
原因,因此堆栈跟踪将显示导致“OTHER”错误的原因。--
其他
名称当然可以是您想要的任何名称,例如
未知异常
内部错误
,等等。如果您确实想抛出新异常,则可以改进MyNewCustomException类。您可以像这样向其中添加一个新的构造函数:抛出新的MyNewCustomException(例如getErrorCode())。或者在创建新StaticsProcessingException的地方,您可以创建一个适当的MyNewCustomException来代替它。另外,您可以在不使用以下输入参数的情况下引发任何异常:MyNewCustomException()@Andreas Yes,我可以引发默认代码,而不是从此处返回null。@zappee我无法编辑StaticsProcessingException,因为它来自库。但是我可以修改“MyNewCustomException”。好吧,那么如果我是你的话,我会将“如果”条件添加到MyNewCustomException类中,只是因为我更喜欢保持“业务”类尽可能清晰:单一责任YMM。那很有趣。最好将所有可能的错误代码存储在映射对象中,并将其用于此特定目的。也许更好:
errMap.getOrDefault(例如getErrorCode(),error.UNKNOWN\u error\u code)