user4351838
user4351838

Reputation:

Try/Except...try everything even if error

I am trying to find a way I can try every statement in a single try statement.

For example:

try:
  funct1()
  funct2()
  funct3()
except:
   print("expected")

The try/except is expected in my case, because one of the functions will fail. How can I do this without multiple try/excepts or what is the best way to do this?

In the current situation, if funct2 fails, funct3 won't run.

Upvotes: 0

Views: 688

Answers (2)

GLHF
GLHF

Reputation: 4035

In my opinion, use try-except blocks in functions. Because, they are catching errors between bunch of codes.In a big program, it will be a problem if you try to catch the all errors in one.So, define your try-except blocks in your functions, more clear and usefull.

Upvotes: 1

user2555451
user2555451

Reputation:

You could put the try/except in a loop:

for funct in (funct1, funct2, funct3):
    try:
       funct()
    except Exception:  # Catch something more specific if you can.
       print("expected")

This will ensure that all of the functions are executed, even if one or more raises an exception.

Upvotes: 4

Related Questions