I have a problem with my code in the try block.
To make it easy this is my code:
try:
code a
code b #if b fails, it should ignore, and go to c.
code c #if c fails, go to d
code d
except:
pass
Is something like this possible?
exceptexceptionpythontry-catch
I have a problem with my code in the try block.
To make it easy this is my code:
try:
code a
code b #if b fails, it should ignore, and go to c.
code c #if c fails, go to d
code d
except:
pass
Is something like this possible?
Best Answer
You'll have to make this separate
try
blocks:This assumes you want to run
code c
only ifcode b
failed.If you need to run
code c
regardless, you need to put thetry
blocks one after the other:I'm using
except ExplicitException
here because it is never a good practice to blindly ignore all exceptions. You'll be ignoringMemoryError
,KeyboardInterrupt
andSystemExit
as well otherwise, which you normally do not want to ignore or intercept without some kind of re-raise or conscious reason for handling those.