Title: How in Python find where exception was raised
Tags: python;exception
Question: How to determine in what function exception was raised. For example exist two functions: 'foo' and 'bar'. In 'foo' exception will raised randomly.

```import random


def foo():
    if random.randint(1, 10) % 2:
        raise Exception
    bar()


def bar():
    raise Exception

try:
    foo()
except Exception as e:
    print "Exception raised in %s" % ???
```

Comment: if this is a purely "academic" question, why not, but you shouldn't rely on stuff like inspecting stack traces in real life code... instead using custom exception classes (or one of the many built-in ones) is better.

Comment: Yes it's only was interesting


Here is another answer: ```import inspect

try:
    foo()
except Exception as e:
    print "Exception raised in %s" % inspect.trace()[-1][3]
```

Comment for this answer: this is probably what the asker meant, but it's probably not the best real life solution :)

Here is another answer: What is your goal? If you are worried about ```bar``` and ```foo``` throwing the same exception type and the caller not being able to differentiate between them, just derive a new exception class:

```import random

class FooException(Exception):
    """An exception thrown only by foo."""

def foo():
    if random.randint(1,10) % 2:
        raise FooException

    bar()

def bar():
    raise Exception

try:
    foo()
except FooException:
    print "Exception raised in foo..."
except:
    print "Exception raised in bar (probably)..."
```

Comment for this answer: I agree your approach is better, but its not working for callbacks that you received as is from users. Or for example RPC when TypeError could be raised in attempt to call function or inside of it.

Actually now I do not build the logic of relying on this information, but it was interesting.
Comment for this answer: In this case you lose information about originally exception if only did not include it in the new
Comment for this answer: You could just wrap each callback in code that raises your custom exception.
Comment for this answer: Well, if you're on Python 3, you can use [exception chaining](http://legacy.python.org/dev/peps/pep-3134/). The way to do this in Python 2 isn't as nice.

Here is another answer: I use the traceback module, like so:

```import traceback

try:
    1 / 0
except Exception:
    print traceback.format_exc()
```

This gives the following output:

```Traceback (most recent call last):
  File "<ipython-input-3-6b05b5b621cb&gt;", line 2, in <module&gt;
    1 / 0
ZeroDivisionError: integer division or modulo by zero
```

If the code runs from a file, the traceback will tell the line and character number of where the error occured :)

EDIT:

To accomodate the comment from Habibutsu: ```Yes, it's useful for printing, but when needed to get more info (for example function name) - not suitable```

The doc-pages tell you how to extract the trace programmatically: http://docs.python.org/2/library/traceback.html

From the page linked above:

```&gt;&gt;&gt; import traceback
&gt;&gt;&gt; def another_function():
...     lumberstack()
...
&gt;&gt;&gt; def lumberstack():
...     traceback.print_stack()
...     print repr(traceback.extract_stack())
...     print repr(traceback.format_stack())
...
&gt;&gt;&gt; another_function()
  File "<doctest&gt;", line 10, in <module&gt;
    another_function()
  File "<doctest&gt;", line 3, in another_function
    lumberstack()
  File "<doctest&gt;", line 6, in lumberstack
    traceback.print_stack()
[('<doctest&gt;', 10, '<module&gt;', 'another_function()'),
 ('<doctest&gt;', 3, 'another_function', 'lumberstack()'),
 ('<doctest&gt;', 7, 'lumberstack', 'print repr(traceback.extract_stack())')]
['  File "<doctest&gt;", line 10, in <module&gt;\n    another_function()\n',
 '  File "<doctest&gt;", line 3, in another_function\n    lumberstack()\n',
 '  File "<doctest&gt;", line 8, in lumberstack\n    print repr(traceback.format_stack())\n']
```

The doc-string for ```traceback.extract_stack``` is the same as for ```traceback.extract_tb```

```
  traceback.extract_tb(traceback[, limit])
  
  Return a list of up to limit “pre-processed” stack trace entries
  extracted from the traceback object traceback. It is useful for
  alternate formatting of stack traces. If limit is omitted or None, all
  entries are extracted. A “pre-processed” stack trace entry is a
  quadruple (filename, line number, function name, text) representing
  the information that is usually printed for a stack trace. The text is
  a string with leading and trailing whitespace stripped; if the source
  is not available it is None.
```

Comment for this answer: Yes, it's useful for printing, but when needed to get more info (for example function name) - not suitable
Comment for this answer: @Habibutsu I updated my answer to show you how `traceback` is indeed suitable, and can be used to extract function names for example :)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/06/14', 'FavoriteCount': 3.0, 'PostTypeId': 1, 'Tags': '<python><exception>', 'Title': 'How in Python find where exception was raised', 'ViewCount': 3748, 'id': 22232201, 'user_AboutMe': '', 'user_AccountId': 1030955.0, 'user_DisplayName': 'Habibutsu', 'user_Reputation': 562.0, 'user_Views': 44.0, 'user_id': 1039267.0}}