How to end flow after Else portion of If statement


#1

I am using an If statement to check if a UI Element is present in my program. If the element is present, then the program selects that UI element and continues the flow.

I want the Else statement (when the UI element is not present) to write to a CSV file as a sort of exception handling case. But after doing this write CSV function, I want the program to end.

Problem is, I am unable to stop the program from the else. It continues down the same path of the Then statement.

How can I branch out the “Then” and “Else” paths to go separate ways?


#2

Hi @cpolicastro,

In this case, I suggest you to separate them into two different sequences instead of using the same sequence and use the Flowchart to decide which way it should follow and where it should end.

I have attached an example for you.
Main.xaml (10.8 KB)


#3

Thanks @acaciomelo. I tried this but the if statement is embedded within a For each row activity and the system doesn’t seem to allow Flow Decisions to be added within a Sequence.


#4

To break out of the loop you can use Break activity. Note this only works for ForEach and ForEachRow (which are essentially the same).


#5

To avoid creating hugely complicated For Each / If statements consider using a Flowchart instead of a sequence and create a loop using an integer which will continue based on the total number of rows in your datatable.


#6

Hi @cpolicastro ,

Put For Each activity in Try catch and in Else part After CSV activity done use Throw activity which will come direct to your Catch block and skip below code.

Hope so this will help you.

Regards,
Udaykumar.


#7

Hi @UdayKumar

Seriously? This is the first time I hear about that technique … #scary


#8

In my opinion Try Catch should be used for handling errors only. As this is not an error I would not advise using this approach but each to their own.

Richard


#9

Hi all,
Yes, Try catch is for Error handling. But to terminate the current sequence we can raise Custom error.
May be this doesn’t sound good. But I think this can help. Correct me if am wrong.

Thanks,
Udaykumar


#10

Using exceptions as control flow is generally considered an anti-pattern, as it’s similar to using GoTo’s in a way.

Keeping that in mind, there are specific scenarios where it could indeed help (also note that BusinessRuleException has been added for a reason).

Going back to original question, it is an error handling situation - write to log + end is a pretty standard behaviour for a catch - and in this specific case I’d argue that throwing an exception is the right call, since implied design is that robot should not handle this situation.

That aside, it’s easy to abuse (since there is no “return”) and desigining a flowchart as @richarddenton suggested could be wiser in the long run (depends on seriousness of the project).


#11

Hi all, thanks for the help in trying to solve this.

I think I am going to go with a Try/Catch that sits outside my entire ForEachRow loop. This will hopefully help write out any exceptions from the loop while continuing the loop for rows even if it needs to kick out exceptions.

Thanks,


#12

(emphasis mine)

Any exception thrown inside the loop will break out of the loop, unless you catch it still inside of it.
So if you want to always process all rows, catching exceptions
This will work:
Foreach dr in dt.Rows Try { something } Catch {your handling code}
This will break out of the loop on first exception:
Try { Foreach dr in dt.Rows // do something } Catch { your handling code }

Regards.


#13

Not contrary to what @andrzej.kniola is suggesting I have to point out that you can terminate the workflow, using terminate workflow activity.

like using a try catch is good, because the element may not be there, so if there is an exception I like the robot send me an email with the details and you can continue from there, like wait 5 minutes or something, I see Andrzej is thinking in the continuity of your process/robot (great approach btw, perhaps you can review the process flow), but if the process needs to stop just right there, and since in this case I think you are using the element exist activity and then the if, right? after the CSV part, use the “Terminate Workflow activity”.

regards.