打印有关异常的所有信息?
Print all information about exception?
我正在开发一些不时执行的后台程序 'something'。但是, 'something' 可能会出错,我想写一个日志,这样我就可以弄清楚出了什么问题,并且可能有不同的错误原因。我正在使用一个封闭源代码库(目前没有可用的文档),我不知道抛出的异常的结构,堆栈跟踪也没有给我足够的信息。
问题:我通常如何使 'all possible information' 对异常可见 class 我 'wish' 不知道?
即有什么方法可以让异常在没有任何进一步知识的情况下自行解释吗?
示例(注意,冗长:-)):
让我们考虑以下银行帐户示例。该帐户有一个函数提供了一个函数 '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);
}
}
}
异常 class 看起来像
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!");
}
在那之前,一切都很好,但让我们说主要代码有很多不同 'requests'(加钱,减钱,发送信息说明帐户中有多少钱,...) .这些请求中的每一个都可能有不同的可能失败情况,例如 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(除了来自他们的名字我不知道如何找到网络无法访问或我们无法向帐户添加一些钱的原因。
所以我认为在抛出异常的地方分派异常然后只创建一个新的异常'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!");
}
}
所以我需要以某种方式将每个可能的异常转换为包含所有异常信息的字符串(即 'custom properties',如 balance 和 amountWithdraw,而不仅仅是 StackTrace)。不过,我没有 know/would 去猜测这些属性。有没有像 toString 一样被覆盖的统一函数?正在做
public void withdrawUnified(double amountWithDraw) throws RequestFailedException{
try {
widthDraw(amountWithDraw);
} catch (Exception e) {
throw new RequestFailedException("Exception=" e);
}
}
不起作用,因为它只打印出堆栈跟踪的第一行。
此致+提前致谢,
转发
你在这里混淆了两件事:.
- 异常首先是一种程序化 方式来传达特定问题的性质。
- 另一方面,也想使用它们从中获取对用户有用的消息。
真正的解决办法是退后一步,把这两个问题清楚地分开。例如,您可以做什么:创建您自己的异常基础 class,并向其添加方法,例如 getErrorMessageForUser()
。现在,您的每个子 class 都可以提供具体的、人类可以理解的错误描述。
同时,您的顶层代码仅捕获该特定异常,然后使用来自该异常的消息。
( 我建议不要使用内置的异常消息。至少在较大的系统中,您很快就会开始考虑国际化,例如:如何确保以特定语言给出错误消息用户 )
如果您打算将自定义异常也用作消息承载者,我建议您覆盖 class 中的 getMessage 以反映您想要说明的失败原因:
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);
}
}
我正在开发一些不时执行的后台程序 'something'。但是, 'something' 可能会出错,我想写一个日志,这样我就可以弄清楚出了什么问题,并且可能有不同的错误原因。我正在使用一个封闭源代码库(目前没有可用的文档),我不知道抛出的异常的结构,堆栈跟踪也没有给我足够的信息。
问题:我通常如何使 'all possible information' 对异常可见 class 我 'wish' 不知道?
即有什么方法可以让异常在没有任何进一步知识的情况下自行解释吗?
示例(注意,冗长:-)):
让我们考虑以下银行帐户示例。该帐户有一个函数提供了一个函数 '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);
}
}
}
异常 class 看起来像
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!");
}
在那之前,一切都很好,但让我们说主要代码有很多不同 'requests'(加钱,减钱,发送信息说明帐户中有多少钱,...) .这些请求中的每一个都可能有不同的可能失败情况,例如 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(除了来自他们的名字我不知道如何找到网络无法访问或我们无法向帐户添加一些钱的原因。
所以我认为在抛出异常的地方分派异常然后只创建一个新的异常'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!");
}
}
所以我需要以某种方式将每个可能的异常转换为包含所有异常信息的字符串(即 'custom properties',如 balance 和 amountWithdraw,而不仅仅是 StackTrace)。不过,我没有 know/would 去猜测这些属性。有没有像 toString 一样被覆盖的统一函数?正在做
public void withdrawUnified(double amountWithDraw) throws RequestFailedException{
try {
widthDraw(amountWithDraw);
} catch (Exception e) {
throw new RequestFailedException("Exception=" e);
}
}
不起作用,因为它只打印出堆栈跟踪的第一行。
此致+提前致谢,
转发
你在这里混淆了两件事:.
- 异常首先是一种程序化 方式来传达特定问题的性质。
- 另一方面,也想使用它们从中获取对用户有用的消息。
真正的解决办法是退后一步,把这两个问题清楚地分开。例如,您可以做什么:创建您自己的异常基础 class,并向其添加方法,例如 getErrorMessageForUser()
。现在,您的每个子 class 都可以提供具体的、人类可以理解的错误描述。
同时,您的顶层代码仅捕获该特定异常,然后使用来自该异常的消息。
( 我建议不要使用内置的异常消息。至少在较大的系统中,您很快就会开始考虑国际化,例如:如何确保以特定语言给出错误消息用户 )
如果您打算将自定义异常也用作消息承载者,我建议您覆盖 class 中的 getMessage 以反映您想要说明的失败原因:
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);
}
}