PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

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

 August 04, 2022     exception, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing