The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 23 Python Implementation of Classes and Objects - Throwing Exceptions
This paper mainly introduces the application scenarios of throwing exceptions, the usage method of the raise statement and the syntax format of the raise statement when using Python Object Oriented programming, and provides corresponding examples to assist the explanation.
This document mainly introduces how to use Python for Object Oriented programming, and requires readers to have a basic understanding of Python syntax and microcontroller development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications, taking the common serial port data transmission and reception, data processing, dynamic graph drawing of host computer and slave computer as application examples, and uses Sourcetrail code software for visual code reading to facilitate readers' understanding.
The link to obtain the relevant sample code is as follows:
https://github.com/leezisheng/Python-OOP-Demo
Body Text
In practical programming scenarios, most methods and constructors impose certain restrictions on the passed parameter values; for example, common index values must be non-negative, and object references cannot be null. You should enforce entry parameter checks at the very beginning of a method, and if an invalid value is input, we can use the raise statement to trigger an exception on our own.
The syntax format of raise is as follows:
raise [Exception [, args [, traceback]]]
In the statement, Exception refers to the exception type (e. g., NameError), which can be any one of the standard exceptions; args denotes the user-provided exception parameters. The last parameter is optional (and rarely used in practice), and if present, it is the traceback exception object.
For example, when creating the SensorClass sensor class, we need to restrict the sensor ID to be between 0 and 99; if this condition is not met, an InvalidIDError exception will be thrown. The sample code is as follows:
class SensorClass(SerialClass):
...
# Class initialization
def __init__(self,port:str = "COM11",id:int = 0,state:int = RESPOND_MODE):
if id <= 0 or id >= 99:
# After an exception is raised, the code after it will not be executed
raise Exception("InvalidIDError:", id)
...
if __name__ == "__main__":
# Create a sensor class with ID 100
s = SensorClass(port = "COM11",
id = 100,
state = SensorClass.RESPOND_MODE
)
The code execution result is as follows:
When an exception is thrown, it appears that the execution of the program stops immediately. All code after the exception throw will not be executed, and unless the exception is handled, the program will exit with an error message. Furthermore, if one function calls another function that throws an exception, all code in the former function after the position where the latter is called will also not be executed. Throwing an exception halts all execution in the function call stack, either being handled properly or causing the interpreter to exit forcibly.