let's say i have some code :
try :
JumpOverFence()
except TooShortError :
print("Wow, you're too short for this obstacle")
ABunchOfLinesThatSaysGoTrain()
except FellError :
print("Wow, you fell! go work on your balance!")
ABunchOfLinesThatSaysGoTrain()
So basically i just have a bunch of lines that i want to run regardless of the error, and a few lines that i want to run for a specific kind of exception, hopefully i was clear.Thanks!
CodePudding user response:
I just thought of the fact that i could use a finally
block where i'll put the "mandatory" part for the execution, because other than in the analogy i made, the code in my use case goes like this :
try :
CopyDataFromAWebsite()
PasteData() #This line won't execute if the website crashes let's say
except WebsiteCrash :
#Print some things
PasteData()
and so what i can do is :
try :
CopyDataFromWebsite()
except WebsiteCrash :
#print some lines
finally :
PasteData()
This way i ensure that this function gets executed whatever happens, but i got to admit, it's a pretty rare usecase..
CodePudding user response:
Write a function.
def GoTrain(msg):
print(msg)
print("Go do some training!")
# more lines here
try :
JumpOverFence()
except TooShortError :
GoTrain("Wow, you're too short for this obstacle")
except FellError :
GoTrain("Wow, you fell! go work on your balance!")