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

发布于 2024-11-17 07:16:47 字数 308 浏览 3 评论 0原文

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

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

如何检查嵌套的 StreamException

Suppose I catch an exception that is of type AppException but I only want to carry out certain actions on that exception if it has a nested exception of type 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?

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

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

发布评论

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

评论(3

柳絮泡泡 2024-11-24 07:16:48

也许您可以尝试为特定目的对 AppException 进行子类化,而不是检查原因。

例如。

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.
}

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

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

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.

红颜悴 2024-11-24 07:16:48
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
    }
}
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
    }
}
錯遇了你 2024-11-24 07:16:47

执行:if (e instanceof AppException 和 e.getCause() instanceof StreamException)

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

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