Python error debugging-raise, assert, PDB

Source: Internet
Author: User
Tags assert

Raise

The Raise statement throws an exception manually: The program does not terminate with an exception, but it runs an error

1 "Raise" [Expression ["," Expression ["," expression] ]

def Fun (x, y    ): if y = = 0        :raise zerodivisionerror ('y besides 0')     return x/ Y>>>fun (2, 0)# error message raise Zerodivisionerror ('y besides 0') zerodivisionerror:y besides 0

Raise followed by the exception to throw, the common exception is as follows:

Exception Name Description
Baseexception base class for all exceptions
Systemexit interpreter Request Exit
keyboardinterrupt user interrupt execution (usually input ^c)
exception base class for general errors
Stopiteration iterator with no more values
generatorexit Generator (generator) exception occurred to notify Exit
Systemexitpython interpreter Request Exit
StandardError base class for all built-in standard exceptions
Arithmeticerror base class for all numeric calculation errors
Floatingpointerror Floating-point calculation error
Overflowerror numeric operation exceeds maximum limit
Zerodivisionerror except (or modulo) 0 (all data types)
Assertionerror Assertion statement failed
Attributeerror object does not have this property
Eoferror has no built-in input to reach the EOF tag
EnvironmentError base class for operating system errors
ioerror input/output operation failed
OSError Operating system error
windowserror system call failed
importerror Import Module/object failed
keyboardinterrupt user interrupt execution (usually input ^c)
Lookuperror base class for invalid data queries
There is no this index (index) in the indexerror sequence
This key is not in the Keyerror map
memoryerror Memory overflow error (not fatal for Python interpreter)
Nameerror Object not declared/initialized (no attributes)
unboundlocalerror access to uninitialized local variables
referenceerror Weak reference (Weak reference) attempts to access objects that have been garbage collected
RuntimeError General run-time errors
Notimplementederror methods that have not yet been implemented
Syntaxerrorpython Syntax error
indentationerror Indent Error
Taberrortab and spaces mixed
SYSTEMERROR General interpreter system error
TypeError operations that are not valid for types
valueerror Invalid arguments passed in
Unicodeerrorunicode Related Errors
Unicodedecodeerrorunicode decoding Error
Unicodeencodeerrorunicode Encoding Error
Unicodetranslateerrorunicode Conversion Error
base class for warning warnings
deprecationwarning warning about deprecated features
futurewarning warning about the change in the construction of future semantics
overflowwarning old warning about auto-promotion to Long integer
pendingdeprecationwarning warnings about attributes that will be discarded
runtimewarning warning for suspicious runtime behavior (runtime behavior)
syntaxwarning warning of suspicious syntax
userwarning warning for user code generation

Using try....except....finally

The Try statement works in the following manner;

    • First, execute the TRY clause (the statement between the keyword try and the keyword except)
    • If no exception occurs, the EXCEPT clause is ignored, and the try clause finishes after execution.
    • If an exception occurs during the execution of a try clause, the remainder of the try clause is ignored. If the type of the exception matches the name after except, then the corresponding except clause is executed. The code after the last try statement is executed.
    • If an exception does not match any of the except, then the exception is passed to the upper try.

A try statement may contain multiple except clauses that handle different specific exceptions. Only one branch will be executed at most.

Finally will be executed when the except is finished.

Try :     = 1/ 0except  zerodivisionerror as E:    print("div besides 0 ")

#结果 Div Besides 0

Python error debugging-raise, assert, PDB

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.