Thursday, August 4, 2022

[FIXED] how to make code run if a exception is caught regardless of it, but have some line for specific exceptions at the same time

Issue

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!


Solution

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!")


Answered By - kindall
Answer Checked By - Senaida (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.