gpt4 book ai didi

java - 等于方法不适用于 Throwable

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:38:22 27 4
gpt4 key购买 nike

我有一些外部提供的回调要运行。因为它们可以包含任何东西,所以我更愿意冒险在它们上捕获 Throwable 并因此从任何可恢复的错误中恢复。

回调执行的某些阶段允许抛出错误,除非错误连续重复两次。在这种情况下,它们将被标记为无效并且无法再运行,除非用户手动启动它们。

这是处理该问题的方法:

  /**
* Sets whether the bot is disabled due to error or not. If error has occured during
* getWindow, the bot will be disabled immediatelly. If the error occured during canRun() or run()
* the bot will only be disabled if the error is repetitive.
* @param error error that occured
* @param phase phase of execution in which the error occured
* @return true if this error is not significant enough to cancel this bot
*/
public boolean continueOnError(Throwable error, ExecutionPhase phase) {
System.err.println("Error "+error+" caught in robot "+this.getClass().getName());
System.err.println("Last: "+lastError+((error.equals(lastError)?" which is the same as last":" which is defferent than last")));
if(phase == ExecutionPhase.GET_WINDOW || (error.equals(lastError) && phase==errorPhase)) {
//Remember last
setLastError(error, phase);
//If robot state listener is listening, inform it about this event
if(listener!=null)
listener.disabledByError(error);
//Disable the robot - on attempt to run, it will throw RobotDisabledException
return !(errorDisabled = true);
}
//Rememeber last
setLastError(error, phase);
//The robot can remain running, but next same error will turn it down
return true;
}

我知道这是一种原始方法,但我需要从某个地方开始。此代码的问题在于 Throwable 上的 equals 方法总是返回 false。查看此方法生成的输出:

Error java.lang.Error: TEST ERROR caught in robot cz.autoclient.robots.LaunchBot
Last: java.lang.Error: TEST ERROR which is defferent than last
Error java.lang.Error: TEST ERROR caught in robot cz.autoclient.robots.LaunchBot
Last: java.lang.Error: TEST ERROR which is defferent than last
Error java.lang.Error: TEST ERROR caught in robot cz.autoclient.robots.LaunchBot
Last: java.lang.Error: TEST ERROR which is defferent than last
Error java.lang.Error: TEST ERROR caught in robot cz.autoclient.robots.LaunchBot
Last: java.lang.Error: TEST ERROR which is defferent than last

为什么会这样?

最佳答案

Throwable 不会覆盖 Objectequals,所以 error.equals(lastError) 的行为与 error == lastError 相同。

也许这对您比较类就足够了:

error.getClass().equals(lastError.getClass())

关于java - 等于方法不适用于 Throwable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29671466/

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