prevent the closure of command Prompt with Python when an "exception" occurs
Question: I have a script in Python 2.7 converted in executable with py2exe. The INPUT
data is a text file where the delimiter need to be valid following this
function:

    
    
    # Check if delimeter is valid
    def get_parse(filename, delimiters=['\t', ',', ' ', ':', ';', '-']):
        with open(filename) as f:
            f.next()
            secondline = f.next()
        for delimiter in delimiters:
            if len(secondline.rstrip().split(delimiter)) >= 3:
                return delimiter
        raise Exception("couldn't find a delimiter that worked!")
    

When the delimiter is not valid (ex: a dot) i am looking for two solution in a
Python elegant way:

  * Until the right INPUT data is not load you can not pass to OUTFILE

or

  * The script break the code, show the error, but the windows (when is a *.exe) doesn't close immediately leaving the user without an explanation

* * *
    
    
    INPUT = raw_input("Input (*.txt): ")
    while not os.path.exists(INPUT):
          print IOError("No such file or directory: %s" % INPUT)
          INPUT = raw_input("Input (*.txt): ")
    try:
        parse = get_parse(INPUT)
    except Exception:
        print ValueError("Delimiter type not valid")
        break
    OUTPUT = raw_input("Output (*.txt): ")
    

with this solution (break) the Window of my *.exe file close leaving the user
without an explanation


Answer: You can hook the exception handler for uncaught exceptions using
[`sys.excepthook`](http://docs.python.org/library/sys.html#sys.excepthook),
and have it call
[`raw_input()`](http://docs.python.org/2/library/functions.html#raw_input) (or
[`input()`](http://docs.python.org/3/library/functions.html#input) in 3.x) as
per [this answer](http://stackoverflow.com/a/1000968/786020).

For a quick example:

    
    
    import sys
    def wait_on_uncaught_exception(type, value, traceback):
        print 'My Error Information'
        print 'Type:', type
        print 'Value:', value
        print 'Traceback:', traceback
        raw_input("Press any key to exit...")
    sys.excepthook=wait_on_uncaught_exception
    

Just modify that to have whatever output or whatever you want (I suggest
looking into the
[`traceback`](http://docs.python.org/2/library/traceback.html) module).

But if you want it more specific to your code, then just put `raw_input("Press
any key to exit...")` in the solution you already have, and it should be fine.
The above should provide a more general solution.

