[英]Making a checked exception a RuntimeException
I'm working on a legacy system that has a custom exception that's used gosh-frickity-everywhere. 我正在使用一个具有自定义异常的遗留系统,该异常使用了gosh-frickity-everywhere。 It was inspired by the
ServletException
class, that said "well any time you have an exception in your Servlet, you'll want to throw this ServletException
". 它受到
ServletException
类的启发,它表示“好吧,只要Servlet中有异常,就要抛出该ServletException
”。
As the system evolved (over 10 years) a more robust system of catching the exceptions at a higher level has taken place and it's no longer necessary to wrap every exception in this custom exception. 随着系统的发展(超过10年),已经出现了一个更强大的系统来捕获更高级别的异常,不再需要将所有异常包装在此自定义异常中。 (One could argue it never was, but that's another story. It's a stable app, so I tend not to complain too much!!) But we're not going to be refactoring them all at once, just slowly over time.
(可以说从来没有,但是那是另一回事了。它是一个稳定的应用程序,所以我不会抱怨太多!!)但是我们不会立即重构它们,只是随着时间的推移会慢慢地重构它们。
However, one thing that would make things simpler going forward would be if the custom exception were a runtime exception instead of a checked exception. 但是,使自定义异常是运行时异常而不是检查异常是使事情变得更简单的一件事。 This way we wouldn't need to explicitly catch it everywhere, and legacy code that hasn't been refactored yet will just continue to throw this the same way as they'd throw a null pointer exception if one occurred.
这样,我们就不需要在任何地方显式捕获它,而尚未重构的旧代码将继续以与抛出空指针异常(如果发生的话)相同的方式抛出此异常。
My question is... What are the side effects of taking a once checked exception and making it a runtime exception? 我的问题是... 接受一次检查的异常并使其成为运行时异常有什么副作用?
I can't think of any, aside from warnings for unnecessary check and throws declarations, but it would be nice to get input from someone who has been down this road before. 除了不必要的警告和引发声明的警告外,我想不出任何办法,但是最好还是从以前走过这条路的人那里得到意见。
Changing a checked exception into an unchecked one has very little practical effect on existing, working code, but you do need to watch out for the possibility that somewhere in your code you catch (RuntimeException ...)
. 将一个已检查的异常更改为一个未检查的异常对现有的工作代码几乎没有实际影响,但是您确实需要注意在代码中某个地方被
catch (RuntimeException ...)
的可能性catch (RuntimeException ...)
。 Such catches do not intercept your custom exception now, but they would do if you make it unchecked. 这样的捕获现在不会拦截您的自定义异常,但是如果您不对其进行检查,它们就会这样做。
You might also run into issues if you do anything reflective related to methods that throw that exception (which apparently is most of them). 如果您做任何与引发该异常的方法相关的事情(这显然是其中的大多数),也可能会遇到问题。
Something like that happened on an old module of a app that I had to maintain. 类似的事情发生在我必须维护的应用程序的旧模块上。 The only problem translating exceptions to runtime is that you may lose granularity but that is entirely up to you to handle.
将异常转换为运行时的唯一问题是您可能会失去粒度,但这完全取决于您的处理。
For example, we had a ton of code like this in the deeper layers: 例如,在更深的层中,我们有大量这样的代码:
catch(IOException e){
Throwables.propagate(e);
}
We used that pattern carelessly all over that layer and, when we needed to check the exception cause, we always had to get the cause of the exception and that made a lot of boilerplate in higher layers. 我们在整个层上都粗心地使用了该模式,并且当我们需要检查异常原因时,我们总是必须找出异常原因,这在高层中产生了很多样板。 To this day I believe it's better to create a good class hierarchy of non-checked exceptions in order to preserve granularity.
到目前为止,我相信最好创建一个良好的非检查异常类层次结构,以保持粒度。 eg
例如
catch(IOException e){
throw new FileNotCreatedException(e);
}
And with this you can catch the exception easily in other layers and divide errors and fallbacks easily: 这样,您可以轻松地在其他层中捕获异常,并轻松划分错误和后备:
catch(FileNotCreatedException e){
notifyError(e);
} catch(NoMoreStorageException e){
releaseSomeStorage();
retryOperation();
}
Here are a few that I can think of 我可以想到的一些
As a general thumb rule in my opinion: 我认为一般的经验法则是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.