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.

Recommended Answers

All 2 Replies

(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."

Just what I was looking for. Thanks heaps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.