5

I have a script with an bail-out function like so:

def die():  
    from sys import exit
    exit()

Occasionally in the script I check a condition and exit if necessary. This works great in 10.1, but unfortunately our server is still at 10. I had to modify the function as follows to work (per this post):

def die():
    from sys import exit
    try:
        exit()
    except SystemExit:
        pass

The problem is that it gets to the except, passes, and keeps on trucking right to the end of the script. Is there any way to actually make the script stop?

7

You're going to need to wrap the whole script in the try:...except SystemExit clause. I fixed that in 10.1, but for 10 you're stuck doing that. Sorry.

2

Are you sure that shouldn't be

def die():
    from sys import exit
    try:
        exit()
    except SystemExit:
        pass
        raise

Since you are intercepting the exception before the main thread, it might not be causing an exit. You also might want to pass an argument to exit(). Any string would work fine.

  • Thanks, I tried this but raising the error causes the script to "fail" (as it did before I added the try clause). Hmm. – Rob Feb 26 '13 at 20:16
  • Now that I see Jason's answer, I understand what you were trying to do :) – blord-castillo Feb 26 '13 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.