How to catch exception but continue Loop Iteration ?

How to catch exception but continue Loop Iteration ?

  

Hi Team,

I am passing a list of records through REST web service and store it in an Out systems entity. This web service will get the records and iterates it in the Foreach loop which splits the values and insert it in multiple entities.  

I have a scenario wherein if any one of these record throws exception I should continue the loop.  

Example: I have 10 records in the list, the 5th record throws exception but still i need to continue the loop iteration with the rest.

Solution

Hi Mehraj,

You can wrap the action you are calling that might throw an exception in an action you create with the 'All Exceptions' exception handler attribute 'Abort Transaction' set to false.

Then where you are looping over this call, replace it with your new wrapped action, so if an exception is thrown, the loop will continue instead of breaking.

So the flow will be to loop over the action, if inside the action an exception is thrown, the exception handler will catch it and do whatever it is you want it to do, with the Abort Transaction set to false, so the loop will just continue moving forward and call the action again.

Let me know if this helps,

Justin

Solution

It works. Thanks for your swift reply.

Sorry to raise this thread, but I have the same problem and I do not understand Justin's response. 

Please could someone explain how to continue the loop, whilst also raising the exception and processing this (I have an error log that I am creating for data remediation)

Decide which action in the loop forces the exception, create a subaction and make sure the exception handler inside this new action is not set to rollback changes, maybe add some logging to the exception handler, call the newly create action inside the loop. Be sure that all changes inside of the subaction will not generate invalid data

Wim van den Brink wrote:

Decide which action in the loop forces the exception, create a subaction and make sure the exception handler inside this new action is not set to rollback changes, maybe add some logging to the exception handler, call the newly create action inside the loop. Be sure that all changes inside of the subaction will not generate invalid data

Thanks for the prompt reply! I had just got it working, actually. But yes your explanation is sound and I appreciate the help :)


Hi Hedge,

You should create a separate action inside the foreach loop. In that action catch the exception and mark the Abort Transaction Property as "NO". Hope this will resolve your problem.


Thanks

Mehraj


Got it, thanks all! 

Joshua Hedge wrote:

Wim van den Brink wrote:

Decide which action in the loop forces the exception, create a subaction and make sure the exception handler inside this new action is not set to rollback changes, maybe add some logging to the exception handler, call the newly create action inside the loop. Be sure that all changes inside of the subaction will not generate invalid data

Thanks for the prompt reply! I had just got it working, actually. But yes your explanation is sound and I appreciate the help :)


Be really sure what you are doing, the roll back system of OutSystems is really good to keep things sound, and preventing this must only be done if you know the consequences. I used it a lot in a system I did build because I wanted to keep track of incoming SOAP calls and be sure the error's happening where not a problem of my system because I was the in-between (like a service bus) between multiple other systems. Also be sure you connect with the caller (if it isn't your system) to get to a feedback from your service so that they will know the call was handled ok, but one or more records were a problem. Otherwise they think everything was handled ok because you don't give an exception as a return from your service..