如何忽略Java中的异常

时间:2015-02-22 15:15:36

标签: java exception exception-handling

我有以下代码:

TestClass test=new TestClass();
test.setSomething1(0);  //could, but probably won't throw Exception
test.setSomething2(0);  //could, but probably won't throw Exception

我想执行:test.setSomething2(0);即使test.setSomething(0)(它上面的行)抛出异常。有没有办法做到这一点除了:

try{
   test.setSomething1(0);
}catch(Exception e){
   //ignore
}
try{
   test.setSomething2(0);
}catch(Exception e){
   //ignore
}

我有很多test.setSomething连续,所有这些都可以抛出异常。如果他们这样做,我只想跳过那一行并转到下一行。

为了澄清,我不在乎它是否抛出异常,我无法编辑抛出此异常的代码的源代码。

这是我不关心例外的情况(请不要使用普遍量化的陈述,例如"你永远不应该忽视异常")。我正在设置一些Object的值。当我向用户呈现值时,无论如何我都会进行空检查,因此如果执行任何代码行,实际上并不重要。

8 个答案:

答案 0 :(得分:17)

我会严重怀疑任何测试代码的完整性,它会忽略测试代码抛出的异常。那就是说,假设你知道自己在做什么......没有办法从根本上忽略抛出的异常。您可以做的最好的事情是最小化将异常抛出代码包装在其中所需的样板文件。

如果您使用的是Java 8,则可以使用:

public static void ignoringExc(RunnableExc r) {
  try { r.run(); } catch (Exception e) { }
}

@FunctionalInterface public interface RunnableExc { void run() throws Exception; }

然后,并暗示静态导入,您的代码变为

ignoringExc(() -> test.setSomething1(0));
ignoringExc(() -> test.setSomething2(0));

答案 1 :(得分:12)

try {
 // Your code...
} catch (Exception ignore) { }

ignore关键字后使用Exception一词。

答案 2 :(得分:2)

您无法忽略Java中的异常。如果方法声明能够抛出某些东西,这是因为无法完成某些重要事情,并且方法设计者无法纠正错误。所以,如果你真的想简化你的生活,请用其他方法封装方法调用:

class MyExceptionFreeClass {
  public static void setSomething1(TestClass t,int v) {
    try {
      t.setSomething1(v);
    } catch (Exception e) {}
  public static void setSomething2(TestClass t,int v) {
    try {
      t.setSomething2(v);
    } catch (Exception e) {}
}

并在需要时调用它:

TestClass test=new TestClass();
MyExceptionFreeClass.setSomething1(test,0);
MyExceptionFreeClass.setSomething2(test,0);

答案 3 :(得分:1)

不幸的是,没有,没有,这是故意的。如果使用得当,不应忽略异常,因为它们表明某些东西不起作用,并且您可能不应该继续沿着正常的执行路径行进。完全忽略例外就是在地毯下扫描它的一个例子。反模式,这就是语言不能轻易支持的原因。

也许您应该看看为什么TestClass.setSomething会抛出异常。无论你想要尝试什么'测试'如果一堆setter方法无法正常工作,真的会有效吗?

答案 4 :(得分:1)

写空捕获块的人将在地狱中永远燃烧。

或更糟糕的是,他们将被迫永远调试他们写的那可恶的垃圾。

也就是说,您可能想做的一件事是以一种不太冗长的方式编写异常处理。 NoException library非常擅长于此。

答案 5 :(得分:0)

你不应该忽视异常。你应该处理它们。如果要简化测试代码,请将try-catch块添加到函数中。忽略异常的最好方法是通过适当的编码来防止异常。

答案 6 :(得分:0)

IntelliJ Idea IDE建议将变量重命名为ignored

不使用时。

这是我的示例代码。

try {
    messageText = rs.getString("msg");
    errorCode = rs.getInt("error_code");
} catch (SQLException ignored) { }

答案 7 :(得分:0)

我知道这很老了,但我确实认为有些情况下您想忽略异常。考虑您有一个字符串,其中包含要分析的定界部分。但是,此字符串有时可以包含6或7或8个部分。我不觉得每次检查len以建立数组中存在的元素都不像捕获异常然后继续那样简单。例如,我有一个以'/'字符分隔的字符串,我想分开:

public String processLine(String inLine) {
    partsArray = inLine.split("/");

    //For brevity, imagine lines here that initialize
    //String elems[0-7] = "";

    //Now, parts array may contains 6, 7, or 8 elements
    //But if less than 8, will throw the exception
    try {
        elem0 = partsArray[0];
        elem1 = partsArray[1];
        elem2 = partsArray[2];
        elem3 = partsArray[3];
        elem4 = partsArray[4];
        elem5 = partsArray[5];
        elem6 = partsArray[6];
        elem7 = partsArray[7];
    catch (ArrayIndexOutOfBoundsException ignored) { }

    //Just to complete the example, we'll append all the values
    //and any values that didn't have parts will still be
    //the value we initialized it to, in this case a space.
    sb.append(elem0).append(elem1).append(elem2)...append(elem7);

    //and return our string of 6, 7, or 8 parts
    //And YES, obviously, this is returning pretty much
    //the same string, minus the delimiter.
    //You would likely do things to those elem values
    //and then return the string in a more formatted way.
    //But was just to put out an example where
    //you really might want to ignore the exception
    return sb.toString();
}
相关问题