Send email on exception during Try/Catch in a loop


#1

Hello,

I have a For Each Row loop using a CSV file. It conducts clicks in a website, prints a file to PDF, and saves it to a local drive based on a specific naming convention specified in the same CSV file.

I want to place the entire loop into a Try / Catch activity and I want the robot to email me when an exception occurs with the row number in the CSV file that caused the exception. I would like for the robot to continue with the loop without interruption or restart the process starting from the row that threw the exception rather than go back to the beginning of the CSV file because that would cause the robot to try to save over an already existing PDF file.

Any guidance is greatly appreciated.

Thank you.


#2

Ok, I could be wrong, but I don’t think you can do this with a For Each Row activity. You will need to convert it to a generic For Each activity or a Do While. (I would recommend For each, but either method would work).

If you use a Do While, you’ll need to use a counter variable and use that counter as your rowNumber
Pseudocode:

declare n = -1
Try
Do Loop
    n = n + 1
    Process dt.Rows(n)
While n < dt.Rows.Count
Catch
    message = "Exception on Rowindex "+n.ToString+", Message: "+exception.Message
    Send SMTP with message

In a For each you can use the IndexOf to store the Row number.
Pseudocode for For each:

declare n = 0
Try
For each row In dt.AsEnumerable.Where(Function(r) r(0).ToString.Trim<>"").ToArray.Skip(n)    ===> TypeArgument: DataRow
    n = dt.Rows.IndexOf(row)
    Process row

Catch
    message = "Exception on Rowindex "+n.ToString+", Message: "+exception.Message
    Send SMTP with message

I hope this is helpful. I just came up with this logic quick so there could be some flaws.

Regards.


#3

All loops will work, but Try-Catch needs to be inside the loop for it to continue.
Although I’d still recommend breaking that process into parts, but that’s another story.


#4

You are right, all loops should work.
Except you can use the Try catch outside just as long as either you use .Skip to you skip the rows you already processed or read a column in the row that tells you that you already processed it.

However, if he breaks it up in separate workflows (like he should, which is another story :P) , he can not use a row index because when an error occurs all arguments don’t get passed back. In that case, he would need to store a process status in a column and filter by that column to continue where it left off… I think anyway

I guess it depends on his framework model on whether he should use the Try/Catch outside or inside the loop. Like if he was Getting the item to process in a part, then processes it in another part, then the Try/Catch could be in the loop, but if the process part is the loop then it could be outside of it.

I don’t think there’s really one right way, but I would probably agree with you that it makes better sense to create a model that gets your item first, then processes it, so you can use the Try/Catch inside the loop.

Thanks.