1

I want to exit Python if I encounter an exception in my function if that function is being executed from the command line, but want to raise an exception and print a stack trace if the my function is not run from the command line.

Right now I have

    try:
        #...
    except Exception as e:
        print('ERROR: Some useful message')
        if __name__ == '__main__':
            raise SystemExit
        else:
            raise e 

but I feel like I'm either doing too much here, or too little.

Is there an idiomatic way to get a stack trace with the original exception when my function is run from the command line; but simply exit if it is being run from the command line?

4
  • 1
    could you rephrase this Is there an idiomatic way to get a stack trace with the original exception when my function is run from the command line; but simply exit if it is being run from the command line?. I'm not sure if that makes. thanks. Commented Feb 27, 2015 at 15:18
  • @HaleemurAli: Fixed, I think. Commented Feb 27, 2015 at 15:21
  • Are you trying to prevent your end users from seeing a traceback? If so, I'm not sure I agree with the wisdom of this strategy. Tracebacks contain useful information for you, the programmer. If the end user can just copy and paste the traceback, it can save you a fair amount of debugging time. Commented Feb 27, 2015 at 15:21
  • @Kevin: It's not that kind of error. Here try is just a test for something. When used from the command line in the context of a isolated command, it could fail without downstream consequences. But if the function has been imported and used, the user importing probably needs to decide for himself whether to exit, so I show the stack trace. Commented Feb 27, 2015 at 15:26

1 Answer 1

4

The better way would be to do this:

import sys

def func():
    do_actual_processing()
    if not successful:
        raise Exception("Yadayada")

if __name__ == '__main__'
    try:
        func()
    except Exception as e:
        sys.exit(1)

That is, the function itself does not need to be concerned with whether or not it is run from command line.

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

6 Comments

Oh wait, I know why: I don't want to do this for all exceptions, or even for all exceptions of a give class. Just for certain exceptions in certain places in the code. E.g., I could have another exception happen somewhere else in my code and not want to exit, ever.
Sounds like bad design. I would do a main function then for command line usage, it really is not a good idea to make a function do something completely different just because __name__ == '__main__'
I think you're looking at this the wrong way (or maybe I'm asking the wrong question): If this is executed from the command line I want to print a message of my own and exit; but if it's not, I want the stack trace and the original exception.
The context is that each of these functions (there are several) corresponds to a command for a script (e.g. myscript.py func). I don't want a full stack trace if that fails; but to just exit with a simple message like "Something I need to proceed isn't here". But if func has been imported and is being used, say, in IPython, I want to be more informative, with the full trace and original exception.
Then why not do as I said, this hardly invalidates my answer.
|

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.