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

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -