Stopping Robot Automatically

Is there a possibility to stop the robot when the processes are failing?

For example, if process has failed for ten times consecutively, I should stop the robot.

Within the RPA Process - you can use PowerShell activity, in that can invoke script like - taskkill /IM process-name /F to kill the Robot Agent in that machine.

But within the process, how can I know how many times my process has failed?