class formaterror(exception)logfile 'formaterror txtdef __init__(selflinefile)self line line self file file def logerror(self)log open(self logfile' 'print('error at:'self fileself linefile=logdef parser()raise formaterror( 'spam txt'if __name__ ='__main__'tryparser(except formaterror as excexc logerror(when runthis script writes its error message to file in response to method calls in the exception handlerc:\codedel formaterror txt :\codepy - excparse py :\codepy - excparse py :\codetype formaterror txt error atspam txt error atspam txt in such classmethods (like logerrormay also be inherited from superclassesand instance attributes (like line and fileprovide place to save state information that provides extra context for use in later method calls moreoverexception classes are free to customize and extend inherited behaviorclass customformaterror(formaterror)def logerror(self)something unique here raise customformaterrorin other wordsbecause they are defined with classesall the benefits of oop that we studied in part vi are available for use with exceptions in python two final notes herefirstthe raised instance object assigned to exc in this code is also available generically as the second item in the result tuple of the sys exc_info(call- tool that returns information about the most recently raised exception this interface must be used if you do not list an exception name in an except clause but still need access to the exception that occurredor to any of its attached state information or methods secondalthough our class' logerror method appends custom message to logfileit could also generate python' standard error message with stack trace using tools in the traceback standard library modulewhich uses traceback objects exception objects