Python error, debugging

Source: Internet
Author: User

1. Error handling

Use try except finally

Try :     = 10/0except  zerodivisionerror, E:    print"zerodivisionerror  ", Efinally:    print"finally "
or modulo by zerofinally

Error type (with warning)

baseexception+--Systemexit+--Keyboardinterrupt+--Generatorexit+--Exception+--stopiteration+--StandardError| +--Buffererror| +--Arithmeticerror|    | +--Floatingpointerror|    | +--Overflowerror|    | +--Zerodivisionerror| +--Assertionerror| +--Attributeerror| +--EnvironmentError|    | +--IOError|    | +--OSError|         | +--windowserror (Windows)|         | +--vmserror (VMS)| +--Eoferror| +--Importerror| +--Lookuperror|    | +--Indexerror|    | +--Keyerror| +--Memoryerror| +--Nameerror|    | +--Unboundlocalerror| +--Referenceerror| +--RuntimeError|    | +--Notimplementederror| +--SyntaxError|    | +--Indentationerror|         | +--Taberror| +--Systemerror| +--TypeError| +--ValueError| +--Unicodeerror| +--Unicodedecodeerror| +--Unicodeencodeerror| +--Unicodetranslateerror+--Warning+--deprecationwarning+--pendingdeprecationwarning+--runtimewarning+--syntaxwarning+--userwarning+--futurewarning+--importwarning+--unicodewarning+--byteswarning

Logging Module Logging error messages

ImportLoggingdefTest (i):Try: J= 10/IexceptStandardError, E:Print "StandardError"logging.exception (e)Print "Test"Test (0)Print "End"
or modulo by Zerotestendtraceback (most recent call last):   " f:/pyproject/test2.py " inch Test     = 10/or modulo by zero

Custom error types inherit built-in error types

Class Fooerror (StandardError):
Pass


def foo (s):
n = Int (s)
if n = = 0:
Raise Fooerror ("value is%s"% s)


Foo ("0")
Traceback (most recent): File"f:/pyproject/test2.py", Line 18,inch<module>Foo ("0") File"f:/pyproject/test2.py", line 15,inchFooRaiseFooerror ("value is%s"%s)__main__. Fooerror:value is0

You can also use raise to throw an error to the top-level caller after you catch an error in except

2. Commissioning

Assert assertion

 def   foo (n): I  = int (n)  assert  i! = 0, "  catch assert   "  return   ia  = foo ( " 0  "  )  print  a 
 traceback (most recent call last): File   f:/pyproject/test2.py   ", line 13, Span style= "COLOR: #0000ff" >in  <module> a  = foo ("  0   " ) File   " f:/ pyproject/test2.py  , line ten, in  Span style= "COLOR: #000000" > foo  assert  i! = 0,  " catch assert  " 
Assertionerror:catch Assert

Equivalent if I! = 0 is true continue execution conversely throw assertionerror and interrupt run

To turn off the Assert method is to add the parameter-O to the Python interpreter to compile the python into a pyo file

Logging will not throw an error but will hit the log the default logging level is warning to modify the default value by Logging.basicconfig (level=logging. Log level)

The logging module prints the logs to standard output and only displays logs that are greater than or equal to the set level (log level level critical > ERROR > WARNING > INFO > DEBUG >notset)

The default format is Logger: output content

Launch the Python debugger pdb, let the program run in a single step, can view the running state at any time

After starting the program with a parameter -m pdb , the PDB navigates to the code to be executed next. Enter a command l to view the code. You can enter a command at any time p 变量名 to view the variable.

Pdb.set_trace () This method also uses the PDB, but does not need to step the execution, we only need import pdb , then, in the possible error place one pdb.set_trace() , can set a breakpoint, the line code, the program automatically pdb.set_trace() pauses and enters the PDB debugging environment, can use the command pview variables or continue running with a command c

Python error, debugging

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.