Hello everyone,
I hope I choose the right category. I am currently automating a test script with UiPath and I want to implement a kind of cleanup to my test case. So in case anything happens (like it can´t find an UiElement) it should give me an error message but also close the browser and do some additional cleanup steps.
I tried to do this with a try catch and including those steps in the final section as they would need to be execued always - so no matter if the test case runs successful or if it runs into an error. But now I have the situation that if it runs into an error it gives me the error in the catch section and also executes the clean up within the final part but get´s shown as green and successfully executed.
Is there any way to have this shown as red? Or another way to do this and without doublicating the cleanup steps in the final and catch part?
Please try this approach
The test as passed because the exception is caught. To fail the test while still running cleanup, use Rethrow in the Catch block and keep all cleanup logic in the Finally block. This is the correct and recommended approach.
ideally it would be passed only as try catch is making the run passed but the verifications and details might fail
now to your situation have a boolean variable created and set it to false at the start of execution and set to true in catch block..now in finally verify if the value is true or not..if true throw an error else dont..then it would be marked failed as error is thrown or add a rethrow in catch also might help here
Place Rethrow in the Catch block and keep cleanup logic in Finally. Finally is always executed unless the workflow is terminated explicitly.
Hope it help
If an exception is handled and not rethrown, the workflow is marked successful. The correct approach is to put cleanup steps in Finally and in Catch log the error and rethrow the exception using Rethrow or Throw. Finally will still execute, but the run will end in a failed state.
I thought so too, but it is not the case or I am doing something wrong.
As soon as I use the rethrow or throw activity in the catch block the final block is not executed.
Finally will always execute even when using Throw or Rethrow, as long as it is inside the same Try Catch Finally block. The correct setup is to log the error in Catch, rethrow it as the last step, and keep all cleanup only in Finally. If Finally is not running, check that you are not using Terminate Workflow, Kill Process, or throwing the exception outside the Try Catch scope.
I am very sorry but it seems I am not smart enough for that. I create a small example test case which has a simple log message in the final to see that the excution went there. Can you please check what I am doing wrong here?
Because it never reaches this log activity as long as I am using the rethrow in the catch block.
Hey @nishiijain2000 ,
this doesn´t work as the final part is skipped as soon as I am using a throw/ rethrow activity.
Check my example here. Maybe I am doing something wrong. TestCase.xaml (15,5 KB)
In UiPath Finally always executes unless workflow is force-terminated. if cleanup is skipped, the issue is usually Terminate workflow or Throw New Exception. Use Rethrow inside Catch and pull all cleanup logic in Finally to ensure proper execution and correct test case status.
Finally cannot be skipped by Throw or Rethrow in UiPath.
This behavior is guaranteed by the .NET runtime.
Finally is skipped only if:
• Terminate Workflow is used
• the exception is thrown from an invoked workflow and not handled in the same workflow
• or Throw / Rethrow is placed outside the Catch block
Please verify that the exception is thrown and caught within the same Try/Catch scope, and that no Terminate Workflow activity is used.
Cheers