always look on the bright side of life raise mybad(traceback (most recent call last)file ""line in __main__ mybadalways look on the bright side of life whatever your method returns is included in error messages for uncaught exceptions and used when exceptions are printed explicitly the method returns hardcoded string here to illustratebut it can also perform arbitrary text processingpossibly using state information attached to the instance object the next section looks at state information options subtle point hereyou generally must redefine __str__ for exception display purposesbecause the built-in exception superclasses already have __str__ methodand __str__ is preferred to __repr__ in some contexts--including error message displays if you define __repr__printing will happily call the built-in superclass' __str__ insteadclass (exception)def __repr__(self)return 'not called!raise ('spam'__main__ espam class (exception)def __str__(self)return 'called!raise ('spam'__main__ ecalledsee for more details on these special operator overloading methods custom data and behavior besides supporting flexible hierarchiesexception classes also provide storage for extra state information as instance attributes as we saw earlierbuilt-in exception superclasses provide default constructor that automatically saves constructor arguments in an instance tuple attribute named args although the default constructor is adequate for many casesfor more custom needs we can provide constructor of our own in additionclasses may define methods for use in handlers that provide precoded exception processing logic providing exception details when an exception is raisedit may cross arbitrary file boundaries--the raise statement that triggers an exception and the try statement that catches it may be in completely different module files it is not generally feasible to store extra details in global exception objects