Java 您如何检查异常的类型以及嵌套异常的类型?

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

How do you check both the exception's type as well as they type of the nested exception?

javaexceptioninstanceof

提问by Timothy

Suppose I catch an exception that is of type AppExceptionbut I only want to carry out certain actions on that exception if it has a nested exception of type StreamException.

假设我捕获了一个类型为 的异常,AppException但我只想对该异常执行某些操作,如果它具有类型为的嵌套异常StreamException

if (e instanceof AppException)
{
    // only handle exception if it contains a
    // nested exception of type 'StreamException'

How do I check for a nested StreamException?

如何检查嵌套StreamException

回答by Marcelo

Do: if (e instanceof AppException and e.getCause() instanceof StreamException).

做:if (e instanceof AppException and e.getCause() instanceof StreamException)

回答by salman.mirghasemi

if (e instanceof AppException) {
    boolean causedByStreamException = false;
    Exception currExp = e;
    while (currExp.getCause() != null){
        currExp = currExp.getCause();
        if (currExp instanceof StreamException){
            causedByStreamException = true;
            break;
        }
    }
    if (causedByStreamException){
       // Write your code here
    }
}

回答by Dunes

Maybe instead of examining the cause you could try subclassing AppException for specific purposes.

也许不是检查原因,您可以尝试将 AppException 子类化以用于特定目的。

eg.

例如。

class StreamException extends AppException {}

try {
    throw new StreamException();
} catch (StreamException e) {
   // treat specifically
} catch (AppException e) {
   // treat generically
   // This will not catch StreamException as it has already been handled 
   // by the previous catch statement.
}

You can find this pattern else where in java too. One is example IOException. It is the superclass for many different types of IOException, including, but not limited to EOFException, FileNotFoundException, and UnknownHostException.

你也可以在java中的其他地方找到这种模式。一个是示例IOException。它是许多不同类型 IOException 的超类,包括但不限于 EOFException、FileNotFoundException 和 UnknownHostException。

回答by crowmagnumb

Apache's ExceptionUtils (org.apache.commons.lang3.exception.ExceptionUtils) is a really great collection of utilities for exception inspection and handling. Included is the method hasCausewhich you could use.

Apache 的 ExceptionUtils ( org.apache.commons.lang3.exception.ExceptionUtils) 是一个非常棒的用于异常检查和处理的实用程序集合。包括hasCause您可以使用的方法。

if (e instanceof AppException && ExceptionUtils.hasCause(ex, StreamException.class)) {
  ...
}

The nice thing about this method is that it will drill down to see if your cause is anywhere in the exception stack trace not just the immediate cause.

这个方法的好处是它会深入查看你的原因是否在异常堆栈跟踪中的任何地方,而不仅仅是直接原因。

Note that hasCausealso checks the passed in exception so if you just want to check if your exception itself is of a type or any of its wrapped exceptions in the chain are of that type you only need the one call.

请注意,hasCause还要检查传入的异常,因此如果您只想检查异常本身是否属于某种类型,或者链中的任何包装异常是否属于该类型,则您只需要一次调用。