How do I catch errors in code loops and exit gracefully?

I am making an app in which I go through a list and try to select certain items (file paths) by programmatically looping through the list. Some times I end up with a bad filepath and the code crashes with an error message. How do I capture the error('if error then do this' kind of code) so that I can recover gracefully without crashing the app?
Thanks in advance.
Dr. Lavanian MD

When asking questions, please ALWAYS post the related/relevant blocks. Also, what error message you get?

It was a generic question and I needed a generic answer. However, here is a specific area where I need an error handling procedure.
I scan a QR code that has a number of fixed items in it (I could have done it in a 'for each from to' loop but did not). This data is extracted and sent to the main application. However, if a qr code with less then expected fields is scanned, an error of running beyond the index is generated. Now if I had an error procedure, it would detect the index overflow problem and exit the afterscan activity at that point. And that is why I am looking for a generic error trapping mechanism that I can use anywhere (like in JScript or Java)

If one assumes that the items missing from the qrcode are always at the end, so 8&9, or 6,7,8,&9, then you can test for/get the length of the list generated by your string split blocks and then use that in a for each loop.

If the items missing from the qrcode are random, e.g. 3,5,&8, then this becomes much more difficult, because from what you show in your blocks you will have no way of knowing which items are missing.

Will you know / do you have a method for identifying which items are missing ?

It is safer and more robust to add extra checks to avoid accidents like this before giving them a chance to happen.
Traffic lights are cheaper than hospital beds.

One solution would be to check the length of the list saved in the variable tmpscan. If the length =/= 9 then show an error message to the user.

1 Like

Yes it is possible to pick out missing items by using an inelegant solution like an 'if then' block to identify a null and action accordingly FOR EACH item! In my present usecase I expect either the 1st field or all 9 fields so i simply used an 'if list items =1 then, else' loop to sort out the issue. But it would lead to issues if some one scanned a random QR code. So what I plan to do is to add an identifier code as the 1st item to my QR codes. Any code not having the ID code would be rejected.
Regrettably I still don't have an answer to
my original question - is it possible to have error trapping in a standard 'built in' format that can be used any where

Are you talking only about the file path? Is it a URL you need to validate?

True, that is why I am searching for an elegant and compact code solution. I am presently able to capture every possible error, but it is at a field level and requires tons of code. (BTW the blocks I have shown above are the original ones. I presently have code that captures both my use cases with an 'if then else' loop.

Not at all, these are simple fields in the QR code, separated by a '^'

I would try this:


thank you. This will do for web pages