try: x="blaabla" y="nnlfa" if x!=y: sys.exit() else: print("error!") except exception: print(exception)
i'm not asking why throwing error. know raises exceptions.systemexit
. wondering if there way exit?
some questions should accompanied real intention behind code. reason problems should solved differently. in body of script, return
can used quit script. point of view, can remember situation in variable , implement wanted behaviour after try/except
construct. or except
may test more explicit kind of exception.
the code below shows 1 variation variable. variable assigned function (the assigned function not called here). function called (via variable) after try/except
:
#!python3 import sys def do_nothing(): print('doing nothing.') def my_exit(): print('sys.exit() called') sys.exit() fn = do_nothing # notice not called. function # given name. try: x = "blaabla" y = "nnlfa" if x != y: fn = my_exit # here different function given name fn. # can directly assign fn = sys.exit; my_exit # adds print visualize. else: print("error!") except exception: print(exception) # function called. or equivalent calling do_nothing(), # or equivalent calling my_exit(). fn()
Comments
Post a Comment