Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use try Except to throw an exception in python

Shulou Source: shulou.com Published: 2022-06-03 02:48:27 09月14日 Update

Try is too useful for python automated testing students.

We need to make sure that each use case gets what we expect and gives the correct pass or fail results in the test report.

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

I. try statement

Usually we use the following format:

Here, when xxxx1 is established, the xxxx3 statement under else is executed.

Execute the xxxx2 statement under except when xxxx1 is not established

Whether xxxx1 is true or not, statements under finally are executed to the

The above formats can be used in the judgment scenario of our use case to get the result of pass or fail.

II. Except type (the type of exception that is thrown)

1. Catch all exceptions:

Try: xxxx1except Exception as e: print (e) # print all exceptions to the screen

2. Catch exceptions of the type:

Try: xxxx1except TypeError as e: print (e) # print type exception to screen

There are many types of exceptions, some of which can write their own exception class and then call it.

Exception type:

Exception name description

The base class of all BaseException exceptions

SystemExit interpreter request exit

KeyboardInterrupt user interrupts execution (usually input ^ C)

Base class of Exception general error

The StopIteration iterator has no more values.

An exception occurred in the GeneratorExit generator (generator) to notify the exit

SystemExit Python interpreter request exit

The base class for all built-in standard exceptions in StandardError

The base class of all numerical calculation errors in ArithmeticError

FloatingPointError floating point calculation error

OverflowError numerical operation exceeds maximum limit

ZeroDivisionError divides (or modulates) zero (all data types)

AssertionError assertion statement failed

The AttributeError object does not have this property

EOFError has no built-in input, arriving at the EOF tag

Base class of EnvironmentError operating system error

IOError input / output operation failed

OSError operating system error

WindowsError system call failed

ImportError failed to import module / object

KeyboardInterrupt user interrupts execution (usually input ^ C)

Base class for LookupError invalid data query

There is no such index in the IndexError sequence (index)

This key is not in the KeyError map

MemoryError memory overflow error (not fatal for Python interpreter)

NameError does not declare / initialize objects (no properties)

UnboundLocalError accesses uninitialized local variables

ReferenceError weak reference (Weak reference) attempts to access objects that have been garbage collected

General runtime errors in RuntimeError

Methods that NotImplementedError has not yet implemented

SyntaxError Python syntax error

IndentationError indent error

TabError Tab mixed with spaces

SystemError general interpreter system error

TypeError operation that is not valid for type

ValueError passes in invalid parameters

UnicodeError Unicode related errors

Errors in UnicodeDecodeError Unicode decoding

Error in UnicodeEncodeError Unicode coding

Error in UnicodeTranslateError Unicode conversion

Base class for Warning warnings

DeprecationWarning warning about deprecated features

FutureWarning warning that the semantics of the construction will change in the future

OverflowWarning's old warning about automatically promoting to long integer (long)

PendingDeprecationWarning warning that features will be discarded

Warning of suspicious runtime behavior (runtime behavior) of RuntimeWarning

Warning of SyntaxWarning's suspicious syntax

Warnings generated by UserWarning user code

Tags: Errors types statements objects systems interpreters interpreters inputs users results suspicions operating systems screens properties values data formats syntax guarantees tests Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL NVidia Shulou Information OPPO Reno