Java:尝试-捕获-继续?

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

Java: Try-Catch-Continue?

javaexception-handlingtry-catch

提问by Legend

Let's say I can a set of statements:

假设我可以使用一组语句:

try {
  String a = getProperty("a");
  String b = getProperty("b");
  String c = getProperty("c");
} catch(Exception e) {

}

Now, lets say property b was not found and the function throws an exception. In this case, how would I just continue or perhaps set b to null without having to write a try-catch block for each property? I mean, a,b,c exist but sometime they might not be found at all during which an exception is thrown.

现在,假设未找到属性 b 并且该函数抛出异常。在这种情况下,我将如何继续或将 b 设置为 null 而不必为每个属性编写 try-catch 块?我的意思是,a、b、c 存在,但有时在抛出异常的过程中可能根本找不到它们。

采纳答案by Xanatos

Assuming you can't change the function so that it returns null when the property isn't found, you are kind of stuck wrapping everything in its own try catch block -- especially if you want for every value that can be retrieved to be retrieved (as opposed to letting the first value that fails cancel the whole operation.)

假设您无法更改函数以使其在未找到该属性时返回 null,那么您将被困在将所有内容包装在其自己的 try catch 块中 - 特别是如果您希望检索每个可以检索的值(与让第一个失败的值取消整个操作相反。)

If you have a lot of these properties to retrieve, perhaps it would be cleaner to write a helper method to use:

如果您有很多这些属性要检索,也许编写一个辅助方法来使用会更清晰:

String getPropertySafely(String key) {
   try {
      return getProperty(key);
   } catch (Exception e) {
      return null;
   }
}

回答by cletus

You have to put a try-catch around each statement. There is no continue (like there is in ON ERROR ... RESUMEblocks in VB). Instead of:

您必须在每个语句周围放置一个 try-catch。没有继续(就像ON ERROR ... RESUME在 VB中的块一样)。代替:

String a = null;
try {
  a = getProperty("a");
} catch(Exception e) {
  ...
}
String b = null;
try {
  b = getProperty("b");
} catch(Exception e) {
  ...
}
String c = null;
try {
  c = getProperty("c");
} catch(Exception e) {
  ...
}

you could write:

你可以写:

public String getPropertyNoException(String name) {
  try {
    return getProperty(name);
  } catch (Exception e) {
    return null;
  }
}

Personally I think a getProperty()is a poor candidate for throwing exceptions just for all this extra boilerplate required

就我个人而言,我认为 agetProperty()是一个糟糕的候选人,只为所有这些额外的样板需要抛出异常

回答by peter.murray.rust

Since you are using the same function each time you might be able to put this in a loop:

由于您每次都使用相同的功能,因此您可以将其放入循环中:

String[] abc = new String[3];
String[] param = {"a", "b", "c"};
for (int i = 0; i < 3; i++) {
    try {
      abc[i] = getProperty(param[i]);
    } catch(Exception e) {

    }
}

but this is rather contrived and would only be useful for a large number of properties. I suspect you will have to simple write 3 try-catch.

但这是相当人为的,只会对大量属性有用。我怀疑您将不得不简单地编写 3 个 try-catch。

回答by Hyman

You should reconsider how getPropertyis handled if you plan to use many of them because there isn't a plain way to do it.

getProperty如果您打算使用其中的许多,您应该重新考虑如何处理,因为没有简单的方法来做到这一点。

You can exploit finallystatement but you still need a try-catchfor every call.

您可以利用finally语句,但try-catch每次调用仍然需要一个。