什么时候可以捕获RuntimeException [英] When is it OK to catch a RuntimeException
问题描述
是否接受任何类型的RuntimeException?如果是的话,其他可以捕获RuntimeExceptions的场景是什么?
你捕获 RuntimeException
因为你捕捉到任何异常的原因:你打算用它做一些事情。也许你可以纠正任何导致异常的事情。也许你只想重新抛出一个不同的异常类型。
捕获和忽略任何异常都是非常糟糕的做法。 / p>
On a recent project I recommended catching a RuntimeException within a test harness code and logging it. The code processes a series of inputs from a database, and I do not want the test to stop due to failure of any one input (Null values, Illegal arguments, etc.). Needless to say, my suggestion triggered a passionate discussion.
Is catching any kind of RuntimeException acceptable? If yes, what are other scenarios where it is OK to catch RuntimeExceptions?
You catch RuntimeException
for the same reason that you catch any exception: You plan to do something with it. Perhaps you can correct whatever caused the exception. Perhaps you simply want to re-throw with a different exception type.
Catching and ignoring any exception, however, is extremely bad practice.
这篇关于什么时候可以捕获RuntimeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!