Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python exception handling

Tags:

python

I am developing a Django site and have been having trouble trying to work out the best way to do exception handling. I have been doing

try:
    Some code
except:
    log error in my own words, i.e 'Some code' failed to execute
    Some other code

This catches all exceptions thus ensuring my site does not deliver 500 errors and such like. But, with my limited knowledge I am losing the actual exception and it is making it a real pain to debug. How do I print the error that occured? Currently I comment out try: catch: and see the error and fix it. There must be a better way!

Thanks in advance

Rich

like image 446
Rich Avatar asked Jan 04 '11 09:01

Rich


People also ask

What are exception handling in Python?

In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause. We can thus choose what operations to perform once we have caught the exception.

What are the 3 major exception types in Python?

There are mainly three kinds of distinguishable errors in Python: syntax errors, exceptions and logical errors.

What is difference between error and exception in Python?

An Error might indicate critical problems that a reasonable application should not try to catch, while an Exception might indicate conditions that an application should try to catch. Errors are a form of an unchecked exception and are irrecoverable like an OutOfMemoryError , which a programmer should not try to handle.


2 Answers

You catch the exception in an exception variable:

try:
    # some code
except Exception, e:
    # Log the exception.

There are various ways to format the exception, the logging module (which I assume you/Django uses) has support to format exceptions, and the exceptions themselves usually render useful messages when rendered to strings.

Here is an example:

import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug('This message should go to the log file')

try:    
    1/0
except Exception as e:
    logging.exception(e)

This example uses the new "as" syntax to catch the exception, supported in Python 2.6 and later. The output of the above is:

DEBUG:root:This message should go to the log file
ERROR:root:integer division or modulo by zero
Traceback (most recent call last):
  File "untitled-1.py", line 6, in <module>
    1/0
ZeroDivisionError: integer division or modulo by zero
like image 182
Lennart Regebro Avatar answered Nov 15 '22 21:11

Lennart Regebro


#!/usr/bin/env python

import sys

try:
    0 / 0
except Exception, e:
    print >> sys.stderr, 'Hello %s' % e
    # Hello integer division or modulo by zero

Note that you can catch multiple exceptions for one block, e.g.:

try:
    open(filename)
except NameError, e:
    print >> sys.stderr, e
except IOError, ioe:
    print >> sys.stderr, ioe

More on exception handling can be found in this tutorial:

  • http://docs.python.org/tutorial/errors.html
like image 36
miku Avatar answered Nov 15 '22 22:11

miku