How do I handle user defined exceptions in Python? -
if have error module defined containing application defined exceptions like:
class error(exception): pass class noschemaversion(error): def __init__(self): self.code = 1 self.msg = "no schema version specified" pass class nomsgtype(error): def __init__(self): self.code = 2 self.msg = "no message type specified" pass
how handle specific exceptions when raised. tried like:
import error errors = error.error() try: <do stuff> except errors.nomsgtype: <stuff>
but message:
attributeerror: 'error' object has no attribute 'nomsgtype'
what doing wrong?
error.error()
(stored in error
) constructs new value of error
class, nomsgtype
separate class isn't part of error
, , error.nomsgtype
doesn't exist. catch nomsgtype
, should write except error.nomsgtype:
.
Comments
Post a Comment