exit explicitly with tools in the sys and os modules sys module exits for examplethe built-in sys exit function ends program when calledand earlier than normalsys exit(nexit with status nelse exits on end of script interestinglythis call really just raises the built-in systemexit exception because of thiswe can catch it as usual to intercept early exits and perform cleanup activitiesif uncaughtthe interpreter exits as usual for instancec:\pp \systempython import sys trysys exit(see alsoos _exittk(quit(except systemexitprint('ignoring exit'ignoring exit programming tools such as debuggers can make use of this hook to avoid shutting down in factexplicitly raising the built-in systemexit exception with python raise statement is equivalent to calling sys exit more realisticallya try block would catch the exit exception raised elsewhere in programthe script in example - for instanceexits from within processing function example - pp \system\exits\testexit_sys py def later()import sys print('bye sys world'sys exit( print('never reached'if __name__ ='__main__'later(running this program as script causes it to exit before the interpreter falls off the end of the file but because sys exit raises python exceptionimporters of its function can trap and override its exit exception or specify finally cleanup block to be run during program exit processingc:\pp \system\exitspython testexit_sys py bye sys world :\pp \system\exitspython from testexit_sys import later trylater(except systemexit parallel system tools