[Tutor] try except block for multiple statements

Lie Ryan lie.1296 at gmail.com
Sun Dec 7 18:25:50 CET 2008


On Mon, 01 Dec 2008 20:44:20 -0500, Bryan Fodness wrote:

> I would like to use a try except to see if a value exists.  But, when I
> use the following, if a does not exist it exits.  I understand why this
> does this, but is there a way to get b,c, and d if a does not exist
> without using a try except for every statement?
> 
> try:
>     fo.write("a = %s\n" %plan.a)
>     fo.write("b = %s\n" %plan.b)
>     fo.write("c = %s\n" %plan.c)
>     fo.write("d = %s\n" %plan.d)
> except AttributeError:
>     pass

Or:
texts = ["a = %s\n" % plan.a, 
         "b = %s\n" % plan.b, 
         "c = %s\n" % plan.c, 
         "d = %s\n" % plan.d
        ]

for text in texts:
    try:
        fo.write(text)
    except AttributeError:
        pass



More information about the Tutor mailing list