First thing: I don't have an answer to your question, and have no idea if it's actually possible to determine if your function is called from a try block :)
That being said, what I'm sure of however, is that it is a weird thing to do. You should leave the decision to raise an exception or fail silently to the caller based either on an explicit parameter or an alternative way of calling your function.
As an example, I believe you should consider how dict
handles missing keys:
- When using
d['missing']
, the dict raises aKeyError
- The caller can choose to use
d.get('missing')
to avoid an exception.