today DotA play well, the article comes relatively simple, hehe ~
The Traceback module is used to track exception return information. As shown in the following example:
Import traceback
try:
raise SyntaxError, "Traceback test"
except:
traceback.print_exc ()
Print out the following results:
Traceback (most recent call last):
File "H:\PythonWorkSpace\Test\src\TracebackTest.py", line 3, in <module>< C6/>raise syntaxerror, "Traceback test"
syntaxerror:traceback test
Or:
Import Traceback
Try: Do
something
except:
print Traceback.format_exc ()
Log.error ( Traceback.format_exc ())
Traceback.print_exc () and print traceback.format_exc () are the same, except that Traceback.print_exc () is printed to the screen, easy to debug, and, of course, can be written to a file.
In Python, there are a number of standard exceptions:
1. Nameerror: Attempt to access an unnamed variable
2. Zerodivisionerror: Divisor is 0
3. SyntaxError: Grammatical errors
4. Indexerror: Index out of range
5. Keyerror: Dictionary keyword does not exist
6. IOError: Input and output error
7. Attributeerror: Accessing Unknown object properties
8. ValueError: Numerical error
9. TypeError: Wrong type
Assertionerror: Assertion Error
Exception: base class for general errors
......
In these standard exceptions, exception is the base class for regular errors, so you can generally use this to catch errors:
Try:
block
except Exception, E:
log.error ("Except,%s"% str (e))
One might ask, except Exception, what does e mean.
Let me tell you this: the except statement follows two things, preceded by the type of exception, followed by an exception object, containing some exception information.
Then one might ask, what does "as" in "except Exception as E" mean.
Let me tell you this: As is an instance of defining an exception (for example, except IOError as E)
Then someone might ask, "except Exception, E" and "except Exception as E" is the relationship.
Let me tell you: the old version of the python,except statement "except Exception, E", Python 2.6 should write "except Exception as E". Recommended use of the latter.