How to throw exception using try except in Python

Source: Internet
Author: User
Tags throw exception

For the Python automation test, the role of try is too great.

We want to make sure that every use case can get what we expect and give the correct pass or fail results in the test report.

It can be implemented well by a try statement, and the result of this pass or fail is guaranteed to be true.

One, try Statement
Usually we will use the following format:

Here, when XXXX1 is established, it executes the XXXX3 statement under else;
Executes the XXXX2 statement under except when XXXX1 is not established
Whether or not the xxxx1 is true, finally the statement is executed to

These formats can be used in the judging scenarios of our use cases to get the result of pass or fail.

Ii. except type (type of exception thrown)
1. Catch all Exceptions:

try:    xxxx1except Exception as e:    print(e) #打印所有异常到屏幕

2. Catch Type exception:

try:    xxxx1except TypeError as e:    print(e) #打印类型异常到屏幕

There are a lot of exception types, and you can write your own exception class and then call it.
Exception type:

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
Systemexit Python interpreter request to 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
SyntaxError Python Syntax error
Indentationerror Indent Error
Taberror Tab and Space mix
Systemerror General Interpreter System error
TypeError operations that are not valid for types
ValueError invalid arguments passed in
Unicodeerror Unicode-related errors
Unicodedecodeerror Unicode decoding Error
Unicodeencodeerror Unicode Encoding Error
Unicodetranslateerror Unicode 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

How to throw exception using try except in Python

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.