Home  >  Article  >  Backend Development  >  Is there any difference between python3 exceptions and errors? (Example analysis)

Is there any difference between python3 exceptions and errors? (Example analysis)

乌拉乌拉~
乌拉乌拉~Original
2018-08-21 19:44:052790browse

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(&#39;Hello world&#39;)
                   ^
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 &#39;spam&#39; is not defined
>>> &#39;2&#39; + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: Can&#39;t convert &#39;int&#39; 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn