java – Equals方法与Throwable不兼容
发布时间:2020-12-15 04:45:37 所属栏目:Java 来源:网络整理
导读:我有一些外部提供的回调来运行.因为它们可以包含任何东西,所以我更愿意冒险抓住Throwable并因此从可恢复的任何错误中恢复. 除非错误连续重复两次,否则允许回调执行的某些阶段抛出错误.在这种情况下,它们被标记为无效,除非用户手动启动它们,否则它们将无法再
我有一些外部提供的回调来运行.因为它们可以包含任何东西,所以我更愿意冒险抓住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 allways上的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不会覆盖Object的等号,因此error.equals(lastError)的行为与error == lastError相同.
也许你比较这些课程就足够了: error.getClass().equals(lastError.getClass()) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |