Is it possible to make python print a specified string if it get an error? -
what mean is, if example error
traceback (most recent call last): file "score_keeper-test.py", line 106, in <module> app = program() file "score_keeper-test.py", line 37, in __init__ self.label1.set_text(team1_name) typeerror: gtk.label.set_text() argument 1 must string, not none
is there way make python print "you must enter name in 2 boxes" instead of error above?
the pythonic idiom eafp: easier ask forgiveness permission. in case:
try: self.label1.set_text(team1_name) except typeerror: print "you must enter name in 2 boxes"
note how explicitly catch typeerror
. recommended pep 8 -- style guide python code (essential reading python programmer):
when catching exceptions, mention specific exceptions whenever possible instead of using bare
except:
clause.
an alternative (not-recommended) approach be:
if team1_name: self.label1.set_text(team1_name) else: print "you must enter name in 2 boxes"
...which example of lbyl: before leap. style can leave code littered in if
-statements.
Comments
Post a Comment