3 ms·
Adding an implicit raise to the end of a bare-except would quietly break things, and is non-trivial to detect. Say you have a naive base-except: def loop()
by cpburns2009 2y ago
Adding an implicit raise to the end of a bare-except would quietly break things, and is non-trivial to detect. Say you have a naive base-except:
def loop():
try:
check_service()
except:
logging.exception("Error while checking service.")
time.sleep(60)
Really you shouldn't be using a base-except here. You should at the bare minimum catch `Exception`. Adding an implicit `raise` at the end will break this function without so much as a warning. Instead of calling the function every minute, the loop is broken with an unexpected exception that was deliberately suppressed (and logged).
A more common scenario for myself is write a lot of my scripts in the style:
def main(argv: list[str]) -> int:
# Parse args, setup logging, etc.
try:
run_script(args)
except:
log.exception("Script failed.")
return 1
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
An implicit raise would obnoxiously break them when my bare-except is intentional, and effectively cause the error to be printed twice to the terminal. Now I'm not wholly opposed to forcing `except BaseException:` instead of `except:`, but an implicit raise would cause all sorts of subtle bugs.