1

Ie, what I want would be:

try: print inExceptClause() 1/0 except Exception: print inExceptClause() print inExceptClause() 

... which would print out:

False True False 
3
  • 6
    Out of curiosity, why would you need such a function? Commented Oct 19, 2012 at 3:36
  • 2
    def inExceptClause(answer=False): return answer, called empty outside of exceptions and with True inside? But guessing you want a program-related indication :) Commented Oct 19, 2012 at 3:40
  • 1
    I'm not sure if such information is available in the interpreter. Also, you can have an except which inside another except, whicn is inside a third except, etc.. The closest thing I can think of is traceback.extract_stack(), but it doesn't give any direct information about excepts. Commented Oct 19, 2012 at 7:15

2 Answers 2

3

I think you're going about this the wrong way. Your "use case" seems like that you can call a function from multiple points in your code, with it sometimes being called from within an exception handler. Within that function, you want to know whether or not an exception has been thrown, right?

The point is, you don't want to have that kind of logic in a function that has (or should have) no knowledge about the calling code... as ideally, most of your functions will not have.

That said, you might want to execute that function, but only partly. So I'd suggest one of two options:

  1. Split up the function into multiple functions: one function has extra functionality, and will in turn call the other function, that has reusable functionality. Just call the function you need, when you need it.

  2. Add a parameter to the function: a simple boolean value might be enough to in- or exclude a small part of that function.

Now, this isn't really an answer to your question, but I have the feeling you are looking at your problem at the wrong angle... hence the above suggestion.

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

1 Comment

I have a logging function which is called at various points in my code, and it would be nice if I could automatically format it slightly differently depending on the context... but I think, ultimately, you're right, trying to detect whether I'm inside an except clause would, in the end, simply be too unreliable. I asked because I thought (if such a thing existed) it might be a quick way to get what I wanted, with almost no code change... but I think perhaps setting some global variable from within my exception manager (or, better yet, using a context manager), would provide a better solution.
0

sys.exc_info()

This function returns a tuple of three values that give information about the exception that is currently being handled. (...) If no exception is being handled anywhere on the stack, a tuple containing three None values is returned.

See also these questions:

Python 3 traceback fails when no exception is active
Raising exceptions when an exception is already present in Python 3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.