File: LP6E/Chapter35/parsely.py
from time import asctime
class FormatError(Exception):
logfile = 'parser-errors.txt'
def __init__(self, line, file):
self.line = line
self.file = file
def logerror(self):
with open(self.logfile, 'a') as log:
print(f'Error at: {self.file} #{self.line} [{asctime()}]', file=log)
def parser(file):
# Parse a file here...
raise FormatError(line=62, file=file)
if __name__ == '__main__':
try:
parser('code.py')
except FormatError as exc:
exc.logerror()