gpt4 book ai didi

java - 打印有关异常的所有信息?

转载 作者:行者123 更新时间:2023-12-05 04:05:24 25 4
gpt4 key购买 nike

我正在开发一些后台程序,时不时地做一些“事情”。但是,“某事”可能会出错,我想写一个日志,这样我就可以弄清楚出了什么问题,并且可能有不同的错误原因。我正在使用一个封闭源代码库(目前没有可用的文档),我不知道抛出的异常的结构,堆栈跟踪也没有给我足够的信息。

问题:对于我“不想”知道的异常类,通常如何才能使“所有可能的信息”可见?

即有没有什么方法可以让异常在没有任何进一步知识的情况下自行解释?

示例(注意,冗长:-)):

让我们考虑以下银行账户示例。该帐户有一个函数提供了一个函数“withdraw”,它显然(或者我应该悲伤地说)抛出异常 InsufficientFundsException:

public class Account {
[...]
private double balance;
public void withdraw(double amountWithDraw) throws InsufficientFundsException {
if(amountWithDraw <= balance) {
balance -= amountWithDraw;
} else {
throw new InsufficientFundsException(balance, amountWithDraw);
}
}
}

异常类看起来像

public class InsufficientFundsException extends Exception {
private double balance;
private double amountWithDraw;

public InsufficientFundsException(double balance, double amountWithDraw) {
this.amountWithDraw = amountWithDraw;
}

public double getBalance() {
return amountWithDraw;
}
public double getAmountWithDraw() {
return amountWithDraw;
}
}

现在在主调用代码中我可能会做类似的事情

try {
account.withdraw(amountWithDraw);
} catch (InsufficientFundsException e) {
System.out.println("Tried to reduce " + e.getBalance() + " by " + e.getgetAmountWithDraw() + " but taking this much amount of money from the account is not possible!");
}

在那之前,一切都很好,但让我们假设主代码收到很多不同的“请求”(加钱、减钱、发送信息以显示帐户中有多少钱,...)。这些请求中的每一个都可能有不同的可能失败情况,例如 AddMoneyException、NetworkUnreachableException,...所以主要代码看起来像

try {
handleRequest(...);
} catch (InsufficientFundsException e) {
System.out.println("Tried to reduce " + e.getBalance() + " by " + e.getgetAmountWithDraw() + " but taking this much amount of money from the account is not possible!");
} catch (NetworkUnreachableException e) {
System.out.println("Network unreachable.");
} catch (AddMoneyException e) {
...
} and so on and so on

因此,一方面,代码变得非常笨拙(我需要通过函数调用堆栈拖拽许多不同的异常),另一方面,我不知道异常 AddMoneyException 和 NetworkUnreachableException(除了它们的名字我不知道如何找到网络无法访问或我们无法向帐户添加一些钱的原因)。

所以我认为在抛出异常时分派(dispatch)异常然后创建一个新的异常“RequestFailedException”是个好主意,然后可以像这样统一处理它:

try {
handleRequest(...);
} catch (RequestFailedException e)
System.out.println("Request failed. Reason=" + e.getReason());
}

现在撤回函数看起来像这样:

public void withdrawUnified(double amountWithDraw) throws RequestFailedException{
try {
widthDraw(amountWithDraw);
} catch (InsufficientFundsException e) {
throw new RequestFailedException("Tried to reduce " + e.getBalance() + " by " + e.getgetAmountWithDraw() + " but taking this much amount of money from the account is not possible!");
} catch (NetworkUnreachableException e) {
throw new RequestFailedException("Network is unreachable!");
}
}

所以我需要以某种方式将每个可能的异常转换为一个字符串,其中包含有关异常的所有信息(即“自定义属性”,如 balance 和 amountWithdraw,而不仅仅是 StackTrace)。但是,我不知道/将不得不猜测这些属性。有没有像 toString 一样被覆盖的统一函数?只是在做

public void withdrawUnified(double amountWithDraw) throws RequestFailedException{
try {
widthDraw(amountWithDraw);
} catch (Exception e) {
throw new RequestFailedException("Exception=" e);
}
}

不起作用,因为它只打印出堆栈跟踪的第一行。

此致+提前致谢,

转发

最佳答案

如果您打算将自定义异常也用作消息承载者,我建议您覆盖类中的 getMessage 以反射(reflect)您想要说明的失败原因:

public class InsufficientFundsException extends Exception {
private double balance;
private double amountWithDraw;

public InsufficientFundsException(double balance, double amountWithDraw) {
this.amountWithDraw = amountWithDraw;
}

public double getBalance() {
return amountWithDraw;
}
public double getAmountWithDraw() {
return amountWithDraw;
}

public String getMessage() {
"Tried to reduce " + balance() + " by " + amountWithDraw() + " but taking this much amount of money from the account is not possible!"
}
}

在你的代码中:

public void withdrawUnified(double amountWithDraw) throws RequestFailedException{
try {
widthDraw(amountWithDraw);
} catch (Exception e) {
throw new RequestFailedException("Request Failed", e);
}
}

关于java - 打印有关异常的所有信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51305955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com