Terminating a python text adventure
Question: so I'm writing a little text adventure and have lots of nested if statements.
However, there comes times where I want to terminate the program in one of
these if statements to give a "GAME OVER". I've tried quit() and exit(), but
at best they still output an error message:

    
    
    Traceback (most recent call last):
      File "<string>", line 9, in 0
    SystemExit: 0
    

I'm not using any try/except statements, just if statements. For example:

    
    
    import sys
    if True:
        print "Hello guv'na"
        if True:
           print "Good day, Good day"
           sys.exit(0)
    if True:
        print "What's all this then?"
    

yields:

    
    
    Hello guv'na
    Good day, Good day
    Traceback (most recent call last):
      File "/Users/austin/test.py", line 6, in 0
    builtins.SystemExit: 0
    

I just want the program to terminate after "Good day, good day" without a
message. is this possible?


Answer: If you are seeing that exception, it means that something is catching and
printing it.

My guess is that you have a try/except block like this:

    
    
    try:
        do_something()
    except:
        print_exc()
    

Here the `except:` clause does not have an exception type specified, which
means that **all** kind of exceptions (including `SystemExit`) will be caught.

Bare `except:` clauses are meant for cleanup code and generally re-raise the
exception after catching it. Avoid them unless you absolutely need them.

Use this instead:

    
    
    try:
        do_something()
    except Exception:
        print_exc()
    

This way, you'll catch only `Exception` subclass, i.e. all standard exceptions
except `SystemExit`, `KeyboardInterrupt` and `GeneratorExit`.

