Over a million developers have joined DZone.

Python: Multiple Exceptions With Parameter

·
I had just began to learn python and struggle about the exception handling of python:


class MyExceptionA(Exception):
    def __init__(self, a):
        Exception.__init__(self)
        self.a = a

class MyExceptionB(Exception):
    def __init__(self, a):
        Exception.__init__(self)
        self.a = a

try:
    print('"Do NOT press this button!" ... oh noes!!! ...')
    raise MyExceptionA(42)
#except MyExceptionA, e: # thats the simple one...
#except (MyExceptionA, MyExceptionB): # must use tuple here...
except (MyExceptionA, MyExceptionB), e:
    if vars().has_key('e'): # just for the middle one :D
        print e.a



Its just that I keep that in mind.
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}