The Java – equals method is incompatible with throwable
I have some externally provided callbacks to run Because they can contain anything, I prefer to risk catching throwable and therefore recovering from any recoverable error
Some stages of callback execution are allowed to throw errors unless the error is repeated twice in a row In this case, they are marked as invalid and will no longer run unless they are manually started by the user
This is the method used to process the method:
/** * 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; }
I know it's a primitive method, but I need to start somewhere The problem with this code is that the equals method on throwable allways returns false To view the output generated by this method:
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
Why is that?
Solution
Throwable does not override the equal sign of the object, so error Equals (lasterror) behaves the same as error = = lasterror
Perhaps it is enough for you to compare these courses:
error.getClass().equals(lastError.getClass())