Python's third-party library series six--traceback library

Source: Internet
Author: User
Tags in python

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.