How do I handle exceptions in Python without blocking execution? -
let within (long) program multiple classes/modules/files, have division 0 error did not deal in try ... except
block. error, know, block program.
is there way log file error forgot handle in order correct program later when check errors logfile ?
try/catch/finally not preferable?
if __name__ == '__main__': try: #my code catch: exc_logger.exception('guys, can fix this?')
if want fancy , use/abuse language features, can place error context catcher within code:
class exceptionrecorder(object): def __init__(self, exc_logger): self.exc_logger = exc_logger def __enter(self): pass def __exit__(self, exc_type, exc_value, traceback): if exc_value: self.exc_logger.error('program terminated following: ', exc_value) return true
and in main:
if __name__ == '__main__': exceptionrecorder(logger.getlogger('oh_no')): #run program code here
take @ how context managers work, __exit__
method.
Comments
Post a Comment