Java / Eclipse - 确定可能的异常而不强制它们?

发布于 2024-12-11 01:30:32 字数 606 浏览 0 评论 0原文

我有您的标准 try/catch 语句:

try
{
  // Do a bunch of stuff
}
catch ( Exception e )
{
  throw e;
}

有没有办法确定可以从我的代码中捕获哪些可能的异常,而不试图强制我的代码失败以查看什么类型的异常 e 是?

例如,如果我做了一些 HTTP 调用或者一些我想要以不同方式处理的 JSON 内容,我的代码可能如下所示:

try
{
  // Do a bunch of stuff
}
catch ( HttpException e )
{
  // Do something
  throw e;
}
catch ( JSONException e )
{
  // Do something else
  throw e;
}
catch ( Exception e )
{
  throw e;
}

但也许我在代码中做了一大堆事情,但我不确定(由于缺乏 Java 经验)可以捕获的可能异常是什么...

有没有可能的方法使用 Eclipse 查看一组代码并获取可以捕获的每种可能类型的异常的列表?

I have your standard try/catch statement:

try
{
  // Do a bunch of stuff
}
catch ( Exception e )
{
  throw e;
}

Is there any way to determine what possible Exceptions could be caught from my code without trying to force my code to fail in order to see what type of Exception e is?

For example, if I did some HTTP calls or maybe some JSON stuff that I want to handle differently, my code might look like this:

try
{
  // Do a bunch of stuff
}
catch ( HttpException e )
{
  // Do something
  throw e;
}
catch ( JSONException e )
{
  // Do something else
  throw e;
}
catch ( Exception e )
{
  throw e;
}

But maybe I'm doing a whole bunch of stuff in my code and I'm not sure (due to lack of Java experience) what the possible Exceptions are that could be caught...

Is there any possible way to use Eclipse to look at a set of code and get a list of every possible type of exception that could be caught?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小情绪 2024-12-18 01:30:32

有两种类型的异常:已检查和未检查。

您无法获得所有未经检查的异常的列表,因为它们可能不会告诉您它们是什么。 (如果您阅读正在使用的库的文档,它们可能会发生。)

对于受检查的异常,如果您没有捕获所有异常,您将无法编译代码。我只需删除 Exception e 块并用它进行编译。诚实地捕获每个异常是一个坏主意,除非您真正准备好处理它。

Well there's two types of exceptions: checked and unchecked.

You can't get a list of all unchecked exceptions, because they might not tell you what they are. (They might, if you read the documentation for the libraries you're using.)

For checked exceptions, you wouldn't be able to compile the code if you weren't catching them all. I would simply remove the Exception e block and compile with it. Honestly catching every exception is a bad idea unless you're truly prepared to handle it.

池木 2024-12-18 01:30:32

无法获取可能的未检查异常的列表。由于它们没有在任何地方声明(它们是动态创建的),所以如果没有非常具体的代码分析工具,这是不可能的——即使这样,它也可能无法从编译的库类中捕获一些。

对于需要预测的棘手事情的示例,请考虑任何分配内存的内容都可能引发内存不足异常,您甚至可以反射性地实例化异常,这几乎是任何静态分析工具都无法找到的。

如果你真的很偏执,你可以捕获 RuntimeException,这应该得到你想要处理的所有未经检查的异常 - 这不是推荐的策略,但可以防止你的程序因某些未知/看不见的未来错误而失败......

这很好表单将任何抛出的异常放入注释中,但这种做法并未得到一致遵循。

There is no way to get a list of possible unchecked exceptions. Since they are not declared anywhere (they are created on the fly) it's just not possible without a pretty specific code analyzer tool--and even then it might not catch some from compiled library classes.

For examples of tricky things to predict consider anything that allocates memory can throw an out of memory exception and you could even instantiate an exception reflectively which would be pretty much impossible to find with ANY static analysis tools.

If you are really really paranoid you could catch RuntimeException, that should get all unchecked exceptions that you would want to deal with--it's not a recommended tactic but can prevent your program from failing from some unknown/unseen future bug...

It's good form to put any thrown exceptions into comments but this practice is not consistently followed.

伪心 2024-12-18 01:30:32

在 NetBeans(也可能是 Eclipse)中,如果您只是输入没有任何 try-catch 块的代码,IDE 将提示您将引发哪些异常,并建议将语句包装在 try-catch 块中。这可能是捕获所有可能的异常的最简单方法。

In NetBeans, and probably Eclipse, if you simply enter the code without any try-catch block, the IDE will hint to you what exceptions will be thrown and offer to wrap the statements in a try-catch block. That's probably your easiest way to catch all the possible Exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文