0

I am new to python language. My problem is I have two python scripts : Automation script A and a main script B. Script A internally calls script B. Script B exits whenever an exception is caught using sys.exit(1) functionality. Now, whenever script B exits it result in exit of script A also. Is there any way to stop exiting script A and continue its rest of execution, even if script B exits.

Thanks in advance.

2
  • Exactly how are you calling scriptB? via import or some sort of syscall? Commented Aug 14, 2013 at 17:40
  • I m calling script by Importing it Commented Aug 14, 2013 at 17:48

2 Answers 2

2

You should encapsulate the code in a try except block. That will catch your exception, and continue executing script A.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. This would work if script B was throwing an exception. However script B is calling sys.exit(1) in my case which causes the program to exit completely.
Except SystemExit: should catch it
1

sys.exit() actually raises a SystemExit exception which is caught and handled by the Python interpreter. All you have to do is put the call into to "script B" into a try/except block that catches SystemExit before it bubbles all the way up. For example:

try:
    script_b.do_stuff()
except SystemExit as e:
    print('Script B exited with return code {0}'.format(e.code))

Comments

Your Answer

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.