Home > Article > Backend Development > Is there any difference between python3 exceptions and errors? (Example analysis)
For those who are exposed to the python programming language for the first time, you can often see some error messages when you first start programming in python, so in this article we will take a look at itPython errors and exceptions related knowledge, and understand the difference between errors and exceptions. Without further ado, let’s get down to business.
There are two types of errors in Python that are easy to identify: syntax errors and exceptions.
Grammar errors
Python grammatical errors, or parsing errors, are often encountered by beginners, as shown in the following examples:
>>>while True print('Hello world') File "<stdin>", line 1, in ? while True print('Hello world') ^ SyntaxError: invalid syntax
In this example, the function print() is detected as having an error because it is missing a colon (:) in front of it.
The parser points out the line in error and marks the first error found with a small arrow.
Exception
Even if the syntax of the Python program is correct, errors may occur when running it. Errors detected during runtime are called exceptions.
Most exceptions will not be handled by the program, and are displayed here in the form of error messages:
>>>10 * (1/0) Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: division by zero >>> 4 + spam*3 Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'spam' is not defined >>> '2' + 2 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: Can't convert 'int' object to str implicitly
Exceptions appear in different types, and these types are printed as part of the information. : The types in the example are ZeroDivisionError, NameError and TypeError.
The front part of the error message shows the context in which the exception occurred, and displays specific information in the form of a call stack.
The above is all the content explained in this article. This article mainly introduces the relevant knowledge of python exceptions and errors. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
For more related knowledge, please visit the Python tutorial column on the php Chinese website.
The above is the detailed content of Is there any difference between python3 exceptions and errors? (Example analysis). For more information, please follow other related articles on the PHP Chinese website!