在调用链上传递异常 [英] Passing an exception up the calling chain

查看:125
本文介绍了在调用链上传递异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望通过在我的方法throws子句中声明异常以及我为什么要这样做来解释通过调用链传递异常意味着什么。

Was hoping for an explanation as to what it means to pass an exception up the calling chain by declaring the exception in my methods throws clause and why I would want to do that.

以下是我对抛出自身异常的理解示例。

Here is an example of my understanding of throwing own exception.

public class ExceptionThrow {
    char[] charArray = new char[] { 'c', 'e', 'a', 'd' };

    void checkArray() throws ABException {
        for (int i = 0; i < charArray.length; i++) {
            switch (charArray[i]) {
            case 'a':
                throw new ABException();
            case 'b':
                throw new ABException();// creating the instance of the
                                        // exception anticipated
            default:
                System.out.println(charArray[i] + " is not A or a B");

            }
        }
    }

    public static void main(String[] args) {
        ExceptionThrow et = new ExceptionThrow();

        try {
            et.checkArray();
        } catch (ABException ab) {
            System.out.println(ab.getMessage() + " An exception did actually occur");
        } finally {
            System.out.println("This block will always execute");
        }

    }
}

class ABException extends Exception {
}

我如何在调用链上传递异常?

How would I pass the exception 'up the calling chain'?

问候
Arian

regards Arian

推荐答案

调用链,通常也称为堆栈跟踪,是所有嵌套方法调用的列表,一行执行。在你的情况下,它的深度为2: main 调用 checkArray ,但可能有很多方法。

The "calling chain", also commonly known as "stack trace", is the list of all nested method calls leading to a single line of execution. In your case, its depth is 2 : main calls checkArray, but there can be dozens of methods.

当代码中发生异常时,它会中断当前方法并将控件返回到堆栈跟踪上的上一个方法。如果此方法可以处理异常(使用 catch ),则将执行 catch ,异常将停止冒泡起来。如果没有,异常将冒出堆栈跟踪。最终,如果它到达 main 并且 main 无法处理它,程序将停止并显示错误。

When an exception occurs in the code, it interrupts the current method and gives the control back to the previous method on the stack trace. If this method can handle the exception (with a catch), the catch will be executed, the exception will stop bubbling up. If not, the exception will bubble up the stack trace. Ultimately, if it arrives in the main and the main cannot handle it, the program will stop with an error.

在您的特定情况下,抛出新的ABException()创建并抛出 ABException 中断 checkArray 方法。然后使用 catch(ABException ab)在您的main中捕获异常。所以根据你的问题,你可以说这段代码在调用链上传递了异常。

In your specific case, the throw new ABException() creates and throws an ABException that interrupts the checkArray method. The exception is then caught in your main with catch(ABException ab). So according to your question, you can say that this code passes the exception 'up the calling chain'.

还有很多话要说,特别是与checked /相关未经检查的例外情况。如果您有更具体的问题,请随时提出。

There are many more things to say, notably related to checked/unchecked exceptions. If you have some more specific questions, feel free to ask.

这篇关于在调用链上传递异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆