Labs / Python OOP

The most beginner-friendly Object Oriented programming tutorial across the entire network: 22 Python implementation of classes and objects - exceptions and syntax errors

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents
The most beginner-friendly Object Oriented programming tutorial across the entire network: 22 Python implementation of classes and objects - exceptions and syntax errors

Abstract:

This article mainly introduces the usage scenarios, definition and features of exceptions, as well as the types and characteristics of errors when using Python Object Oriented programming, and provides practical examples to assist the explanation.

FreakStudio's Blog

You who study embedded systems still don't know Object Oriented? ?!

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 00 Introduction to Object Oriented Design Methodology

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 01 Basic Concepts of Object Oriented Programming

The Most Beginner-Friendly Object Oriented Programming Tutorial Online: 02 Python Implementation of Classes and Objects - Creating Classes with Python

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 03 Python Implementation of Classes and Objects - Adding Attributes to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 04 Python Implementation of Classes and Objects – Adding Methods to Custom Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 05 Python Implementation of Classes and Objects - PyCharm Code Tab

The most beginner-friendly Object Oriented programming tutorial on the entire network: 06 Python implementation of classes and objects - data encapsulation of custom classes

The most beginner-friendly Object Oriented programming tutorial across the entire network: 07 Python implementation of classes and objects - type annotations

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Web: 08 Python Implementation of Classes and Objects - @property Decorator

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 09 Python Implementation of Classes and Objects - Relationships Between Classes

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 10 Python Implementation of Classes and Objects - Class Inheritance and Liskov Substitution Principle

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 11 Python Implementation of Classes and Objects - Subclass Calling Parent Class Methods

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 12 Python Implementation of Classes and Objects - Using Python's logging Module to Output Program Runtime Logs

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 13 Python Implementation of Classes and Objects – Installation and Usage of Sourcetrail, a Fantastic Tool for Visual Code Reading

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 14 Python Implementation of Classes and Objects – Static Methods and Class Methods of Classes

The most beginner-friendly Object Oriented programming tutorial across the network: 15 Python implementation of classes and objects - the __slots__ magic method

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 16 Python Implementation of Classes and Objects - Polymorphism, Method Overriding and Open Closed Principle

The most beginner-friendly Object Oriented programming tutorial across the entire network: 17 Python implementation of classes and objects - duck typing and "file-like object"

The most beginner-friendly Object Oriented programming tutorial across the entire network: 18 Python implementation of classes and objects - multiple inheritance and serial port data curve plotting with PyQtGraph

The Most Beginner-Friendly Object Oriented Programming Tutorial on the Whole Network: 19 Python Implementation of Classes and Objects – Using PyCharm to Automatically Generate File Comments and function Comments

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 20. Python Implementation of Classes and Objects – Implementation of Composition Relationship and CSV File Saving

The Most Beginner-Friendly Object Oriented Programming Tutorial Across the Network: 21 Python Implementation of Classes and Objects - Multi-File Organization: Module and Package

More exciting content is available here:

Speed Up Your Python: A Quick Guide to Python Parallel Computing

Understand the Debugging Principle of CM3 Microcontroller in One Article

After half a month of dedicated work, the comprehensive summary of embedded technology stack is now released

The "Secret Martial Arts Manual" for Computer Science Competitions

Document and Code Acquisition:

You can download the document via the following link:

https://github.com/leezisheng/Doc

19

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

Programs are fragile in most cases; only when all conditions meet the ideal state can the code consistently produce correct results. However, in practical applications, it is sometimes impossible to guarantee that all computations will yield correct outcomes. For example, in team collaboration, some passes 0 into the divisor parameter when calling the data division method/function we wrote, causing the program to crash, or accesses the 8th element in a list that only has a length of 5. In such cases, we need to hold a core viewpoint: treat everyone like a thief on guard, they are all rotten apples!!!If something goes wrong with your code, it is not the responsibility of the function/method caller — it must be your own responsibility. You must ensure that no matter what others pass in, your code will not malfunction; if you cannot handle it, throw an exception.

Next, I'll walk you through how to use exception handling to stop bad actors from passing the buck:

Actually, an exception is an event that occurs during the execution of a program and disrupts the normal flow of the program's instructions. Generally, an exception is raised when Python encounters a situation it cannot handle properly. For example, in the following program, we perform a division-by-zero operation:

5 0 1

A ZeroDivisionError exception has occurred. This exception is raised when an attempt is made to divide by zero. When we encounter such an abnormal situation that prevents normal operation, we need to promptly throw, catch and handle the exception.

Errors in Python can be (at least) divided into two categories: syntax errors and exceptions.

while True print('Hello world')
  File "<stdin>", line 1
    while True print('Hello world')
               ^^^^^
SyntaxError: invalid syntax

The parser will repeat the line containing the error and display an 'arrow' pointing to the token in that line where the error was detected. The error may be caused by a missing token before the token being pointed to. In this example, the error was detected on the function print (), because a colon (':') was missing before it. The filename and line number will also be printed so that you know where to look for the problem when the input comes from a script.

10 * (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
4 + spam*3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
'2' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

The last line of the error message indicates what type of error the program has encountered. There are different types of exceptions, and the type name is printed as part of the error message: the exception types in the above examples are ZeroDivisionError, NameError, and TypeError in sequence. The string printed as the exception type is the name of the built-in exception that occurred. This holds true for all built-in exceptions, but not necessarily for user-defined exceptions (although this convention is very useful). Standard exception types are built-in identifiers (not reserved keywords).

The rest of this line describes the error details based on the exception type and the cause of the error.

The error message starts with a stack trace to show the context where the exception occurred. It generally lists the stack trace of the source code lines, but will not display the lines read from standard input.

347622978