Re: How to get the Exception Type

2012-05-15 Thread bruno desthuilliers
On May 15, 1:29 am, LJ  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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How to get the Exception Type

2012-05-15 Thread bruno desthuilliers
On May 15, 2:31 am, Nikolas Stevenson-Molnar 
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


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How to get the Exception Type

2012-05-14 Thread Nikolas Stevenson-Molnar
You could also log the whole stack trace using the traceback module:
http://docs.python.org/library/traceback.html

_Nik

On 5/14/2012 4:54 PM, Luis Gonzalez wrote:
> 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 # '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.
>
> On 14 mayo, 18:29, LJ  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)
>>
>> How do I catch all types of exceptions?  And, how do I get the
>> exception type?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How to get the Exception Type

2012-05-14 Thread Luis Gonzalez
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 #'
  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.

On 14 mayo, 18:29, LJ  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)
>
> How do I catch all types of exceptions?  And, how do I get the
> exception type?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



How to get the Exception Type

2012-05-14 Thread LJ
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?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.