title: getting the exception class name in python?
body: <p>I want to generate a string in an exception handler that contains the name of the exception, and any arguments passed...oretty much the final output that one gets with Traceback.</p>
  
  <p>For example, if <code>raise bar.FnordError("message")</code> is called, in the exception handler, I want to produce the string: <code>"bar.FnordError: message"</code></p>
  
  <p>I want it to work for built in exceptions, as well as exceptions in the current and other modules.  This is what I came up with, but it doesn't seem very pythonic.</p>
  
  <pre><code>def this_is_lame(err):
      if type(err).__module__ in ['__main__', 'builtins']:
          return "{}: {}".format(type(err).__name__, err)
      else:
          return "{}.{}: {}".format(type(err).__module__, type(err).__name__, err)
  </code></pre>
  
  <p>I've dug through the BaseException C code in python, and the Traceback standard library. I seem to be missing the correct "nice" accessor.</p>
  
  <p>Is <code>BaseException.__format__</code> documented anywhere?  Are there escapes for it?</p>
  
  <p>I've played around in the interpreter and nothing quite gives me what I want.</p>
  
  <pre><code>import sys
  import traceback
  
  import bar
  
  try:
      raise bar.FnordError("message")
  except Exception as err:
      print(type(err))
      print(repr(err))
      print(type(err).__module__)
      print(type(err).__name__)
      print(err)
      print("this is what I want: '{}'".format(this_is_lame(err)))
  
  print()
  
  try:
      raise ValueError("message")
  except Exception as err:
      print(type(err))
      print(repr(err))
      print(type(err).__module__)
      print(type(err).__name__)
      print("this is what I want: '{}'".format(this_is_lame(err)))
  </code></pre>
  
  <p>which produces:</p>
  
  <pre><code>$ python foo.py
  &lt;class 'bar.FnordError'&gt;
  FnordError('message',)
  bar
  FnordError
  message
  this is what I want: 'bar.FnordError: message'
  
  &lt;class 'ValueError'&gt;
  ValueError('message',)
  builtins
  ValueError
  this is what I want: 'ValueError: message'
  </code></pre>