954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Re: Catching Exceptions

On the risk of sounding extremely stupid, Does anyone know how to print a string representation of an exception. i.e. just the type of error? I have code that catches all errors and would like to then print the type of error as a string. My code is:

def catchException():
    try:
        f [6]
    except:
        print "This is where I want to print the string representation of the error message"
    else:
        print "No exception raised."


Any help would be appreciated.

Thanks.

charper
Newbie Poster
2 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

(More about exceptions here: http://docs.python.org/tut/node10.html )

Does this do what you want:

def catchException():
    try:
        f[6]
    except Exception, inst:
        print inst
    else:
        print "No exception raised."
Mike W
Newbie Poster
4 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

Just what I was looking for. Thanks heaps

charper
Newbie Poster
2 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You