Cleanup if test case fales and correct execution status

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?

Thanks a lot in advance!

Greetings,
Sabine

Hi @sabine.klemenjak

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.

Cheers

@sabine.klemenjak

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

cheers

Tried that already, but as soon as I use a throw or rethrow activity the final block is completly skiped and not executed at all.

can we assume a testcase XAML?

you can check for

  • deferred (re)throwing the exception
  • deferred evaluation /verification of a present error with a verify expression activity

Yes it is a xmal test case.
Throw and rethrow doesn´t work for my as this than leads to the issue that the final block gets skipped.

With checking you mean verify if there was/ is an error in the final block and than set the outcome of the test case?

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

Hi @sabine.klemenjak,

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.

Thanks

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.

Do you maybe have an example where it works?

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.

TestCase.xaml (15,5 KB)

nicht unbedingt, hängt davon wie ab originär du den Fehler braucht Meldung oder Stack

you can defer it as mentioned as you will assign the excption to a variable and at the end after final block you can handle it by

E.g
if isNothing(myExceptionVar)

  • Then:
    • throw an exception with e.g. message from myExceptionVar.Message

Or
using the Verify Expression Activity as mentioned above

UPD2 - moved added visualizations below

So I need to go for a work around as the throw/ rethrow is skipping the final, right?

Find some visuals:



Variante A (also good to work with new Exception and nested old exception :

Variante B (Vorschlag, zum weiteren Ausbau)

Hi @sabine.klemenjak,

Use Try-Catch-Finally then Put all cleanup login in finally in catch, log the error and rethrow the exception

Catch ex as Exception
LogMessage(ex.message, LogLevel.Error)
Throw
Finally
CloseBrowser()
End Finally()

Hey @Bob01,
This is sadly not working. As soon as I use a throw activity the final part will be skipped so my cleanup won´t be executed.

Greetings,
Sabine

Hi @sabine.klemenjak
Try the approach which I suggest hope it’s help
Cheers

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)

Hii @sabine.klemenjak ,

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.

Hi @sabine.klemenjak

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