java 如何知道抛出了哪个异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7586305/
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-30 20:36:12  来源:igfitidea点击:

How to know which exception is thrown

javaexceptionexception-handling

提问by Mouna Cheikhna

I'm doing a review for our codebase, and there are many statements like this:

我正在我们的代码库,有很多这样的陈述:

try
{
   doSomething()
} catch (Exception e)
{

}

but I would like a way to know which exception is thrown by doSomething() (there's no throw statement in the implementation of doSomething) so i can catch that exception instead of just catching Exception in general, even with findBugs it gives a warning REC_CATCH_EXCEPTION.

但我想要一种方法来知道 doSomething() 抛出了哪个异常(doSomething 的实现中没有 throw 语句)所以我可以捕获该异常而不是一般只捕获 Exception ,即使使用 findBugs 它也会发出警告REC_CATCH_EXCEPTION

I should mention that logging the exception or printing it to console will not help me because it takes time in this case to reproduce the error that causes the exception here.

我应该提到记录异常或将其打印到控制台对我没有帮助,因为在这种情况下需要时间来重现导致此处异常的错误。

Thanks

谢谢

回答by tb189

If there's no throwsstatement in doSomething(e.g. doSomething() throws IOException), any exceptions that will occur will be an instance of RuntimeException. If you want to know the exact class of an exception thrown by doSomething, you can always try

如果(eg ) 中没有throws语句,则将发生的任何异常都将是. 如果您想知道由 抛出的异常的确切类别,您可以随时尝试doSomethingdoSomething() throws IOExceptionRuntimeExceptiondoSomething

try {
   doSomething();
} catch (RuntimeException e){
   System.out.println(e.getClass().getName());
}

Knowing which runtime exceptions can be thrown without actually running the program is difficult. Even if none of the code that doSomething()calls has an explicit throw, core java operations can always throw NullPointerException, ArrayIndexOutOfBoundsException, etc with the wrong input. Here are some ideas:

知道可以在不实际运行程序的情况下抛出哪些运行时异常是很困难的。即使没有该代码的doSomething()调用具有显式罚球,核心Java操作可总是抛出NullPointerExceptionArrayIndexOutOfBoundsException等用了错误的输入。这里有一些想法:

  • Dig through manually. At least you will know some of the exceptions.
  • Use reflection to find any throw statements accessible from doSomething.
  • Run your test cases and log the exceptions thrown like above. Good tests will reveal the important errors that callers of doSomethingshould be ready for.
  • Go to the people who put the catch there in the first place
  • 手动挖掘。至少你会知道一些例外情况。
  • 使用反射查找可从doSomething.
  • 运行您的测试用例并记录上面抛出的异常。好的测试将揭示调用者doSomething应该准备好应对的重要错误。
  • 去找那些把鱼放在首位的人

In any case it's usually a good idea to catch exceptions that are as specific as possible, since you don't know exactly whatwent wrong when you try to deal with all cases in one clause.

在任何情况下,捕获尽可能具体的异常通常是个好主意,因为当您尝试在一个子句中处理所有情况时,您并不确切知道出了什么问题。

回答by Ryan Stewart

You can 1) dig through all the code in doSomething() and everything it calls to see the exception handling and what RuntimeExceptions can be thrown, or 2) take of the catch (Exception e)and wait for it to fail. This is the problem that checked exceptions attempted to overcome by making a strongly-typed declaration in method signatures about what exceptions must be handled as a result of calling the method.

您可以 1) 浏览 doSomething() 中的所有代码及其调用的所有内容,以查看异常处理以及可以抛出哪些 RuntimeExceptions,或者 2) 执行catch (Exception e)并等待它失败。这是检查异常试图通过在方法签名中进行强类型声明来解决的问题,该声明关于调用该方法的结果必须处理哪些异常。

回答by michael667

If there is no throwsclause, then the method does not throw any checkedexecption. The javadoc of that method might give information about any uncheckedexceptions the method can throw, but it does not have to.

如果没有throws子句,则该方法不会抛出任何已检查的执行。该方法的 javadoc 可能会提供有关该方法可以抛出的任何未经检查的异常的信息,但它不是必须的。

Why do you want to catch any exceptions in the first place?

为什么首先要捕获任何异常?

回答by JW8

Do you want to catch just compile-time exceptions or run-time exceptions as well? If you only want to catch the compile-time ones, just remove your current catch block - you'll immediately get error messages stating that certain exceptions aren't being caught. After you add one exception, you'll see error messages for others - you'll be able to finish catching all possible exceptions thrown by your code block.

您是否只想捕获编译时异常或运行时异常?如果您只想捕获编译时的那些,只需删除当前的 catch 块 - 您将立即收到错误消息,指出某些异常没有被捕获。添加一个异常后,您将看到其他人的错误消息 - 您将能够完成捕获代码块抛出的所有可能异常。