简体   繁体   English

为错误的配置抛出Unchecked Exception是一个好主意吗?

[英]Is it a good idea to throw an Unchecked Exception for bad configuration?

My application uses a config file with a lot of configurable items in it. 我的应用程序使用其中包含很多可配置项的配置文件。

For instance, you can specify a custom keystore with a custom alias to be used for WebService connections (instead of the default JVM javax.net.ssl.keystore ). 例如,您可以使用自定义别名指定要用于WebService连接的自定义密钥库(而不是默认的JVM javax.net.ssl.keystore )。

During runtime, we might discover that this alias does not exist in the keystore, so we might want to throw an exception. 在运行时,我们可能会发现此别名在密钥库中不存在,因此我们可能想抛出一个异常。 Because this is a crucial part of the application (and we can't expect the application to function properly until the config is fixed), I'm thinking throwing an Unchecked Exception is a good idea here. 因为这是应用程序的关键部分(并且我们不能期望应用程序在配置被修复之前才能正常运行),所以我认为抛出Unchecked Exception是一个好主意。

Am I right in thinking that way? 我这样想对吗?

Would it make sense to create a custom ConfigurationException (which extends RuntimeException ) to throw in this case? 在这种情况下,创建一个自定义ConfigurationException (扩展了RuntimeException )是否有意义?

Checked Exceptions are thrown to notify the users of your methods that the method can throw a particular exception. 引发检查异常会通知您的方法用户该方法可以引发特定异常。 So that the users can decide what to do when we get that exception. 这样,当我们收到该异常时,用户可以决定要做什么。

If your requirement is "Application should not proceed unless the configurations are correct" and there is no method user can run to circumvent it (in try-catch block) then I do not see any reason to make it a checked Exception (Why to make users do unnecessary work if we know it is of no use). 如果您的要求是“除非配置正确,否则应用程序不应继续运行”,并且用户无法运行任何方法来绕过它(在try-catch块中),那么我看不出有任何理由使其成为已检查的异常(为什么要这样做如果我们知道没有用,用户将执行不必要的工作)。

So, in this case I would also go for unchecked Exception. 因此,在这种情况下,我还将寻求未经检查的异常。

I think you should create a checked exception and when you catch it, end the program in catch block after displaying a message or something. 我认为您应该创建一个检查异常,并在捕获它时在显示消息或其他内容后在catch块中结束程序。 Unchecked exceptions are for programming errors and should be corrected by the developer. 未经检查的异常是针对编程错误的,开发人员应予以纠正。 Check exceptions are for conditions that might happen but we have no control over it. 检查异常是针对可能发生的情况,但我们无法控制它。 You know the config file may be corrupted but you cannot prevent it from happening and cannot control it. 您知道配置文件可能已损坏,但是您无法阻止它发生并且无法控制它。 I would go with checked exception. 我会去检查异常。

Unchecked exception indicate a bug in the program or a system error. 未检查的异常表示程序中的错误或系统错误。 For example, ArrayIndexOutOfBoundsException or NullPointerException shouldn't occur in a program, and if they occur, they usually indicate a bug. 例如, ArrayIndexOutOfBoundsExceptionNullPointerException不应在程序中发生,并且如果发生,则通常表示错误。

When validating user input, either direct user input or user input via a configuration file, an exception does not indicate a bug. 验证用户输入(直接用户输入或通过配置文件输入用户)时,异常并不表示错误。 Therefore it should be a checked exception, to remind the programmer who calls your method that an exception is not unlikely to occur and must be caught. 因此,它应该是一个已检查的异常,以提醒调用您的方法的程序员,异常不太可能发生并且必须被捕获。 By catching the exception you can produce a user-friendly error message which points to a solution. 通过捕获异常,您可以生成用户友好的错误消息,该错误消息指向解决方案。 (You don't want the end user to see the Java-generated Exception in thread ... message!) (您不希望最终用户Exception in thread ...消息中看到Java生成的Exception in thread ... !)

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

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