Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use try Except to throw an exception in python

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report