Is there any difference between the Python3 exception and the error? (Instance parsing)

Source: Internet
Author: User
Tags stdin
For the first time in contact with the Python programming language of the friend, just start Python programming often can see some of the error message, so in this article we will come to understand python errors and exceptionsKnowledge, and understand the difference between an error and an exception。 No more nonsense, let's get to the point.

Python has two types of errors that are easily recognizable: syntax errors and exceptions.

Syntax error

The syntax error of Python, or it is called parsing fault, is often encountered by beginners, the following example:

>>>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 checked for errors because a colon (:) is missing in front of it.

The parser points to the wrong line and marks a small arrow at the location of the first found error.

Abnormal

Even if the syntax of the Python program is correct, an error may occur when you run it. The run-time detected error is called an exception.

Most exceptions are not handled by the program, and are presented here in the form of an error message:

>>>10 * (1/0) Traceback (most recent):  File ' <stdin> ', line 1, in? Zerodivisionerror:division by zero>>> 4 + spam*3traceback (most recent call last):  File "<stdin>", Lin E 1, in?  Nameerror:name ' spam ' isn't defined>>> ' 2 ' + 2Traceback (most recent call last):  File "<stdin>", line 1, in? Typeerror:can ' t convert ' int ' object to str implicitly

Exceptions occur in different types, which are printed as part of the information: the types in the examples are zerodivisionerror,nameerror and TypeError.

The previous section of the error message shows the context in which the exception occurred and displays the specific information as the call stack.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.