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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -