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?

asked Feb 26, 2013 at 19:27

2 Answers 2

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.

answered Feb 26, 2013 at 20:48
0
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.

answered Feb 26, 2013 at 19:51
2
  • Thanks, I tried this but raising the error causes the script to "fail" (as it did before I added the try clause). Hmm. Commented Feb 26, 2013 at 20:16
  • Now that I see Jason's answer, I understand what you were trying to do :) Commented Feb 26, 2013 at 22:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.