Summary of common built-in exception knowledge points in Python|Dry goods

We should also often encounter it when coding in normal times, and we will often report exceptions in the process of typing the code.

Obviously, part of the reason may be that the program gives an error message due to your own carelessness, of course, it may also be an exception caused by the program's operation.

Therefore, it is necessary to actively capture the exceptions that occur to prevent the program from terminating due to error messages.

So first of all, we need to know that built-in exceptions can be generated by the interpreter or built-in functions. And it is divided into: specific exceptions, OS exceptions, and warning categories.

Here are some examples of exceptions that are often raised.

Specific exception

1. AssertionError
will be raised when the assert statement fails. When a user uses an assertion statement to detect an exception, if the expression detected by the assertion statement is false, such an exception will be raised.

2. KeyError
KeyError is a keyword error, which will cause an error when the specified mapping (dictionary) key cannot be found in the existing key set. This exception mainly occurs in dictionaries, such as when a user tries to access a key that does not exist in the dictionary.

3. NameError
NameError is raised when a local or global name is not found, that is, an error occurs in the variable name, such as when the user tries to call a variable that has not been assigned or initialized, it will be triggered.

4. ValueError
When an operation or function receives a parameter with the correct type but an inappropriate value, it is also a value error. For example, you want to get the index of a non-existent value in a list.

5. SystemError
is raised when the interpreter finds an internal error, but the situation does not seem serious enough to give up all hope. The associated value is a string (represented as a low-level symbol) that indicates what happened.

6, SyntaxError
SyntaxError is mainly because when the parser encounters a syntax error, such as fewer colons, multiple quotation marks, etc., a slight negligence during programming will make an error. It should be the most common type of abnormal error.

7. TypeError
TypeError is a type error, which will be raised when an operation or function is applied to an object of an inappropriate type. For example, when an int is requested but a list is passed in, it will cause an error.

8. IndexError
will be raised when the sequence extraction is out of range, that is, the index is out of range. For example, the most common subscript index exceeds the sequence boundary. For example, when a sequence m has only three elements, it tries to access m[4].

9. StopIteration
StopIteration is an iterator error, which is caused by the built-in function next() and the next () method of iterator to indicate that the iterator cannot produce the next item. When the access to the last value of the iterator continues to access, this exception will be thrown.

10. AttributeError
AttributeError is an attribute error, which appears when the attribute reference or assignment fails. For example, a list has an index method, but a dictionary does not, so calling this method on a dictionary object will cause the exception.

OS abnormal

1. FileNotFoundError
will be raised when the requested file or directory does not exist, that is, an error will occur when the user tries to open a non-existent file in read mode.

2. TimeoutError
will be raised when a system function has a system-level timeout.

3. FileExistsError
will be raised when trying to create an existing file or directory.

Warning exception

1.
The base class of exception Warning warning category.

2, exception UserWarning
The base class for warnings generated by user code.

3. Exception FutureWarning
If the warning issued is for the end user of the application written in Python, use this as the base class for warnings related to deprecated features.

4. Exception ImportWarning is
the base class for warnings related to possible errors in module import.

I still want to recommend the Python learning group I built by myself : 645415122 , all of whom are learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and shares dry goods from time to time (only Python software development related), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021, welcome to advance and useless to
**the following content, this blog is crawled and used by search engines
(*  ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄) What
is it? How long does it take to learn python with zero basics? Why is python called crawler
python? crawler rookie tutorial python crawler universal code python how to make money crawler
python basic tutorial web crawler python python crawler classic example
python crawler
(* ̄︶ ̄)(* ̄︶ ̄)(* ̄ ︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)(  ̄︶ ̄)(  ̄︶ ̄) The
above content is useless, this blog is used by search engines

Guess you like

Origin blog.csdn.net/m0_55479420/article/details/115183910