如何附加到 Java 异常?

发布于 2024-10-27 12:06:04 字数 859 浏览 1 评论 0原文

我对 Java 和一般的异常都很陌生。

在我之前的 C/Perl 编程时代,当我编写库函数时,错误会通过布尔标志以及某种带有人类友好(或程序员友好)错误消息的字符串传回。 Java 和 C++ 有异常,这很方便,因为它们包含堆栈跟踪。

我经常发现,当我捕获异常时,我想添加我的两分钱,然后将其传递出去。

这怎么能做到呢?我不想丢弃整个堆栈跟踪......我不知道失败发生的深度以及原因。

我有一个小实用程序库可以将堆栈跟踪(从异常对象)转换为字符串。我想我可以将其附加到我的新异常消息中,但这看起来像是一个黑客。

下面是一个示例方法。建议?


    public void foo(String[] input_array) {
        for (int i = 0; i < input_array.length; ++i) {
            String input = input_array[i];
            try {
                bar(input);
            }
            catch (Exception e) {
                throw new Exception("Failed to process input [" 
                        + ((null == input) ? "null" : input)
                        + "] at index " + i + ": " + Arrays.toString(input_array) 
                        + "\n" + e);
            }
        }
    }

I am new to Java and exceptions in general.

In my prior C/Perl programming days, when I wrote a library function, errors were passed back by a boolean flag, plus some kind of string with a human-friendly (or programmer-friendly) error message. Java and C++ have exceptions, which is handy because they include stack traces.

I often find when I catch an exception, I want to add my two cents, then pass it along.

How can this be done? I don't want to throw away the whole stack trace... I don't know how deep the failure occurred and for what reason.

I have a little utility library to convert a stack track (from an Exception object) into a string. I guess I could append this to my new exception message, but it seems like a hack.

Below is an example method. Advices?


    public void foo(String[] input_array) {
        for (int i = 0; i < input_array.length; ++i) {
            String input = input_array[i];
            try {
                bar(input);
            }
            catch (Exception e) {
                throw new Exception("Failed to process input [" 
                        + ((null == input) ? "null" : input)
                        + "] at index " + i + ": " + Arrays.toString(input_array) 
                        + "\n" + e);
            }
        }
    }

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

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

发布评论

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

评论(4

最丧也最甜 2024-11-03 12:06:04

异常可以被链接起来:

try {
    ...
} catch (Exception ex) {
    throw new Exception("Something bad happened", ex);
}

它使原始异常成为新异常的原因。可以使用 getCause() 获取异常原因,并对新异常调用 printStackTrace() 将会打印:

Something bad happened
... its stacktrace ...
Caused by:
... original exception, its stacktrace and causes ...

Exceptions can be chained:

try {
    ...
} catch (Exception ex) {
    throw new Exception("Something bad happened", ex);
}

It makes original exception the cause of the new one. Cause of exception can be obtained using getCause(), and calling printStackTrace() on the new exception will print:

Something bad happened
... its stacktrace ...
Caused by:
... original exception, its stacktrace and causes ...
梦忆晨望 2024-11-03 12:06:04

通常,您会抛出一个新异常,其中包含异常作为“原因”。大多数异常类都有一个接受“原因”异常的构造函数。 (您可以通过 Throwable.getCause() 来实现这一点。)

请注意,您几乎永远不应该只捕获 Exception - 通常您应该捕获更具体类型的异常。

Typically you throw a new exception which includes the old exception as a "cause". Most exception classes have a constructor which accept a "cause" exception. (You can get at this via Throwable.getCause().)

Note that you should almost never be catching just Exception - generally you should be catching a more specific type of exception.

强者自强 2024-11-03 12:06:04

只需使用不同的构造函数:

Exception(String message, Throwable cause)

消息是您的“两分钱”,并且您包含捕获的异常,该异常将显示在堆栈跟踪打印输出中

Just use a different constructor:

Exception(String message, Throwable cause)

The message is your "two cent" and you include the catched exception, which will be shown in a stacktrace printout

穿透光 2024-11-03 12:06:04

您可以将根本原因添加到新创建的异常中:

    throw new Exception("Failed to process input [" 
                + ((null == input) ? "null" : input)
                + "] at index " + i + ": " + Arrays.toString(input_array) 
                + "\n" + e.getMessage(), e);

You can add the underlying cause to a newly created exception:

    throw new Exception("Failed to process input [" 
                + ((null == input) ? "null" : input)
                + "] at index " + i + ": " + Arrays.toString(input_array) 
                + "\n" + e.getMessage(), e);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文