How to get the Exception Type

160 views
Skip to first unread message

LJ

unread,
May 14, 2012, 7:29:17 PM5/14/12
to Django users
I am trying to add exception handling to my application.
I have been reading the documentation on Django Exceptions:
https://docs.djangoproject.com/en/dev/ref/exceptions/
I also found the list of Exceptions (both Django and Python).
But for testing purposes, I need a way to handle all exceptions and to
log the exception types:

try:
addresses = Address.objects.all()
except Exception:
logger.debug(Exception.Type)

How do I catch all types of exceptions? And, how do I get the
exception type?

Luis Gonzalez

unread,
May 14, 2012, 7:54:23 PM5/14/12
to Django users
Hi as you can see here at http://docs.python.org/tutorial/errors.html
in 8.3 handling exceptions theres a piece of code that works as you
want.

for example try this:

try:
int("a")
except Exception as e: #The key is using "as" and the variable name
you like for retrieving the exception because if you dont you will
only get
#"Exception type" type

print type(e) #This will give de exception type. In this case #<type
'exceptions.ValueError>'
print e #This will print you the message exception. #invalid
literal for int() with base 10: 'a'
print e.message #This would give you the string message #invalid
literal for int() with base 10: 'a'


I hope this can help you, please forgive me for my grammar im mexican.

Nikolas Stevenson-Molnar

unread,
May 14, 2012, 8:31:18 PM5/14/12
to django...@googlegroups.com
You could also log the whole stack trace using the traceback module:
http://docs.python.org/library/traceback.html

_Nik

bruno desthuilliers

unread,
May 15, 2012, 5:41:12 AM5/15/12
to Django users
On May 15, 2:31 am, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> You could also log the whole stack trace using the traceback module:http://docs.python.org/library/traceback.html

Or just use logging.exception, that takes care of all the gory details
in a single and simple call:

try:
foo = bar.baaz()
except Exception, e: # 'exception as e' won't work with older python
versions
logger.exception("oops")
raise # if you don't handle the exception, let it propagate, thanks


bruno desthuilliers

unread,
May 15, 2012, 5:46:15 AM5/15/12
to Django users
On May 15, 1:29 am, LJ <ljayad...@gmail.com> wrote:
> I am trying to add exception handling to my application.
> I have been reading the documentation on Django Exceptions:https://docs.djangoproject.com/en/dev/ref/exceptions/
> I also found the list of Exceptions (both Django and Python).
> But for testing purposes, I need a way to handle all exceptions and to
> log the exception types:
>
> try:
>    addresses = Address.objects.all()
> except Exception:
>   logger.debug(Exception.Type)

try:
addresses = Address.objects.all()
except Exception, e:
# cheap
logger.debug("got %s" % type(e))
# better since you'll have the whole traceback
logger.exception("oops : %s" % e)
raise # IMPORTANT

Reply all
Reply to author
Forward
0 new messages