Hi everyone!
I wanted to find a way to skip to the next item within a Switch activity, if an error occurs. Currently, if the error occurs, the automation just stops. Is there a way to accomplish this?
Hi
Usually switch enables you to select one choice out of multiple, based on the value of a specified expression
It chooses item on its own based on the expression and there won’t be any issue in that because if it couldn’t find the case after validating the expression then it goes to DEFAULT part of switch activity
May be once after finding the case after validating the expression and inside that case if any activity fails it can handled by surrounding those activities with a TRY CATCH Block
That is within each case in switch activity make sure the activities are kept insid the try catch block
So that once entering any case inside switch activity and if any thing goes wrong it will let the bot to execute further and won’t stop
Cheers @sidb
Thank you @Palaniyappan
I had tried a Try/Catch already, but unfortunately when the bot faults on the first Case, it does not move on to the next Case. It might be because it’s on a different workflow when it faults.
Obviously it won’t go to next case any of the case fails
But it will get out of the switch activity itself
So if you want the bot to go to next case if any case fails then use a FLOW CHART with FLOW SWITCH instead of sequence with switch activity
It is possible to include a flow chart inside a sequence so give a try with that
Cheers @sidb
I thought the point of a Try/Catch was to catch when an error happens?
Hi @sidb,
As @Palaniyappan mentioned that is how the switch activity is supposed to work. Even if you have a try/catch, the execution will stop after a condition is met or no conditions are met.
Your case is a prime candidate for the newly released activity called the Else If activity. In the Else If activity will try each condition until it is satisfied. But the catch is that this is only available in the newest version of UiPath 21.10 and forward.
In this release post you can see the Else-If activity : UiPath Community 2021.10 Stable Release - Studio - Help / Studio - UiPath Community Forum
I see that the documentation currently is quite little for this activity : Else If (uipath.com)
To solve it with the switch statement, you can try this option.
Not saying I like it from a best practice point of view, but it should work.
- Create a do while loop, with condition like [exception = true] in whichever way you want to measure that.
- Within the body a try catch.
→ try contains your switch statement. The switch condition would be easiest as an integer matching option 1 as 1, 2 as 2 etc… So translate the actual conditions before the switch into this integer set.
→ The catch handles your error. HEre you increment your switch integer, and your exception condition to true.
Result: through the error the do while loop continues back into the same switch activity, but with an incremented switch condition resulting in hopping to the next option.
It’s a bit of dirty coding, and there are a few scenario’s you need to cover to prevent endless loops, but it’ll give you what you desire.
Exactly it works in that way only
But it will act only on the activities which Try block surrounds
Say for example you have three activities in sequence
Click activity
Type into activity
Send hot key activity
Say the type into activity is surrounded by try catch
And the rest are kept as it is without any try catch
Now if the click activity fails then it will stop the bot
But if type into activity fails it won’t stop and it will let the bot to proceed further to send hot key activity
Similarly in switch activity inside each case if the activities are surrounded with try catch and if any exception occurs it will leave the switch activity itself and goes for next activity and not next case inside switch
Because only one case in switch is chosen by switch activity itself based on the expression
Being so it won’t be able to move to multiple cases if one case fails
It will go out of switch activity itself
Hope this clarifies
And the reason why suggested for flow chart is you can direct the chain to next activity very easily
Have a look on this doc for more details about that activity
Cheers @sidb
Not sure if you tried try catch for entire switch or for the cases inside it.
If there are 3 cases in switch statement then add the try catch for each case inside that case’s workflow that will handle the error occurred in that case, may be if needed you can get the error in a out variable. this works for the scenario you are mentioning…
Share the workflow if possible.