【问题标题】:How to check which exception type was thrown in Java?如何检查Java中抛出了哪种异常类型?
【发布时间】:2014-12-03 20:23:49
【问题描述】:

如果一个操作捕获多个异常,我如何确定捕获了哪种类型的异常?

这个例子应该更有意义:

try {
  int x = doSomething();
} catch (NotAnInt | ParseError e) {
  if (/* thrown error is NotAnInt */) {    // line 5
    // printSomething
  } else {
    // print something else
  }
}

在第 5 行,如何检查捕获了哪个异常?

我尝试了if (e.equals(NotAnInt.class)) {..},但没有成功。

注意:NotAnIntParseError 是我项目中扩展 Exception 的类。

【问题讨论】:

标签: java exception-handling


【解决方案1】:

如果多个throws 发生在一个catch() 中,那么要识别哪个异常,您可以使用instanceof 运算符。

javainstanceof运算符用于测试对象是否为指定类型(类或子类或接口)的实例。 em>

试试这个代码:-

        catch (Exception e) {
            if(e instanceof NotAnInt){

            // Your Logic.

            } else if  if(e instanceof ParseError){                

             //Your Logic.
            }
      }

【讨论】:

  • 这与您写下答案 4 年后接受的答案中所写的内容有何不同?
  • @GhostCat 谢谢!有这么多的复制粘贴帖子,像这样混乱的问题......
  • 这个答案与公认的(并且有帮助的)不同,因为它专注于一个解决方案,该解决方案在示例中被隐藏并且在答案文本中没有提及或解释。单独的 catch 块对我的问题不起作用,因为我正在查看单个异常类型的根本原因,所以我略读了这些答案。
  • 这正是我要找的!我有一个多重捕获,仅针对一种异常类型,我想偏离正常行为。
【解决方案2】:

如果可以,总是为个别异常类型使用单独的 catch 块,没有理由不这样做:

} catch (NotAnInt e) {
    // handling for NotAnInt
} catch (ParseError e) {
    // handling for ParseError
}

...除非您需要共享一些共同的步骤,并且出于简洁的原因想要避免使用其他方法:

} catch (NotAnInt | ParseError e) {
    // a step or two in common to both cases
    if (e instanceof NotAnInt) {
        // handling for NotAnInt
    } else  {
        // handling for ParseError
    }
    // potentially another step or two in common to both cases
}

然而,共同的步骤也可以提取到避免if-else块的方法:

} catch (NotAnInt e) {
    inCommon1(e);
    // handling for NotAnInt
    inCommon2(e);
} catch (ParseError e) {
    inCommon1(e);
    // handling for ParseError
    inCommon2(e);
}

private void inCommon1(e) {
    // several steps
    // common to
    // both cases
}
private void inCommon2(e) {
    // several steps
    // common to
    // both cases
}

【讨论】:

  • 什么情况下不能放多个catch块?只是好奇。
  • @Chargnn 我也想知道。也许catch 块正在调用一些onException(Exception e) 回调?恕我直言,另一个答案更好。
  • 抱歉,我最近对帖子进行了有害的编辑,现在我已恢复(并进一步改进)。
【解决方案3】:

如果有人不知道方法中抛出了什么类型的异常,例如有很多可能性的方法,比如这个:

public void onError(Throwable e) {

}

你可以通过

获取异常类
       Log.e("Class Name", e.getClass().getSimpleName());

在我的例子中是UnknownHostException

然后使用前面答案中提到的instanseof 采取一些措施

public void onError(Throwable e) {
       Log.e("Class Name", e.getClass().getSimpleName());

       if (e instanceof UnknownHostException)
            Toast.makeText(context , "Couldn't reach the server", Toast.LENGTH_LONG).show();
       else 
          // do another thing
}

【讨论】:

    【解决方案4】:

    使用多个 catch 块,每个异常一个块:

    try {
       int x = doSomething();
    }
    catch (NotAnInt e) {
        // print something
    }
    catch (ParseError e){
        // print something else
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 2016-11-10
      • 1970-01-01
      相关资源
      最近更新 更多