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.