1

I have a code which runs mutile python codes such as below:

execfile("1.py")
execfile("2.py")
execfile("3.py")

however occasionally one of the above codes as an error, i put exit('error') in the code to cancell if there is an error. However i want the rest of the code to run and exit('error') exits the whole code, not just the execfile. How do i get the execfile to stop but the others to keep running?

The part of 1.py with exit() is:

try :
    Ntot=10000
    x,y,s=myMCMC2D(Ntot,0.78,0.63,1,1)
except :
    exit('error')
astrochris
  • 1,756
  • 5
  • 20
  • 42
  • 3
    This reminds me of the old joke. "doctor, it hurts when I do this." "Well, then, stop doing that". If you don't want your programs to exit when they encounter an error, then don't call `exit` in the except clause in your programs. – Kevin Oct 15 '14 at 12:27

1 Answers1

3
try:
    execfile('1.py')
except SystemExit:
    print "1.py exited"

Exit is an exception which can be caught.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93