python - jupyter: how to stop execution on errors? -
the common way defensively abort execution in python like:
if something_went_wrong: print("error message: goodbye cruel world") exit(1) however, not practice when using jupyter notebook, seems abort kernel entirely, not wanted. there proper/better way in jupyter, besides hack-y inifinte loops?
no, exit() not way abort python execution usually. exit() meant stop interpreter status code.
usually write script that:
if __name__ == '__main__': sys.exit(main()) try not put sys.exit() in middle of code bad practice, , might end non closed filehandle or locked resources.
to want, raise exception of right type, , if propagate eval loop, ipython stop notebook execution.
plus give useful error messages , stack trace.
if type(age) not int: raise typeerror("age must integer") elif age < 0: raise valueerror("sorry can't born in future") else : ... you can inspect stack post-mortem %debug , see went wrong where, that's subject.
Comments
Post a Comment