0

I have the following python code;

try:
    write_file(param="A")
    write_file(param="B")
    write_file(param="C")
except ValueError:   

If the first write_file raises exception, I want the code to continue to the next write_file function. Currently, it runs to the end and stops.

I can't use the answers from a similar question python catch exception and continue try block That question uses different function whereas my question uses the same function but with different parameters each time.

I am using python v3.7

user3848207
  • 3,737
  • 17
  • 59
  • 104
  • Just turn it around, `for param in ("A", "B", "C"):` then try/except *inside* the loop. That's exactly what the other question's answers show, it doesn't matter that you're looping over a parameter rather than the function. – jonrsharpe May 17 '20 at 11:34
  • @jonrsharpe, sorry. I'm a bit slow. Would you mind writing an answer? – user3848207 May 17 '20 at 11:35
  • 1
    On second thoughts, I think I get what you mean now. Let me try out. thanks. – user3848207 May 17 '20 at 11:36
  • @jonrsharpe, One thing I don't understand. Why use `("A", "B", "C")` and not list or set? – user3848207 May 17 '20 at 11:40
  • 1
    Why not? List or tuple is fine (see https://stackoverflow.com/questions/25368337/tuple-or-list-when-using-in-in-an-if-clause), a set isn't semantically ordered so may not make sense. – jonrsharpe May 17 '20 at 11:42

1 Answers1

0

I will answer my own question. The credit goes to jonrsharpe who dropped a hint in the comments.

    params = ("A", "B", "C")
    for param in params:
        try:
            write_file(param=param)
        except ValueError as ve:
user3848207
  • 3,737
  • 17
  • 59
  • 104