Sixth chapter errors and anomalies
Until now the error message has not been involved. But if you have tried the example you may have seen some relevant information. There are at least two different kinds of errors in Python: syntax errors and exceptions
6.1 Syntax errors (syntax errors)
Syntax errors, which are compile errors, may be your most common complaint when you start learning python.
>>> while True print (' Hello world ')
File "<stdin>", line 1, in?
While True print (' Hello world ')
^
Syntaxerror:invalid syntax
The parser knows the error statement and displays a small "arrow" before the location where the error is checked. The error is caused by the character of the arrow editing or at least detected. In the example, an error was detected on the function print () because there was one missing in front of it:. From the text entry, the file name and number of lines will be printed, so you can know where to locate the error.
8.2 Exceptions
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/
If statements and expressions are syntactically correct, an error may occur when you try to run it. Errors detected during execution are called exceptions and the program does not collapse unconditionally. Next you'll learn how to handle exceptions in a python program. However, many exceptions cannot be processed by the program, causing the following error message:
>>> 10* (1/0)
Traceback (most recent call last):
File "<stdin>", line 1, in?
Zerodivisionerror:int division or modulo 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
The last line of the error message describes the cause of the error, the exception is produced in different ways, and the error type is printed as part of the message: In the instance, the error message is Zerodivisionerror,nameerror and TypeError. The string that is printed as the error type is the name that appears in the built-in exception. This is a normal result for built-in exceptions, but it is not known for user-defined exceptions, although this is a useful specification. The name of the standard exception is a built-in identifier. (This is not a reserved keyword).
The rest of the line information provides more detailed information based on the type of exception and the cause.
The previous section of the error message displays the context in which the exception occurred and prints it as a stack. In general, it contains a list of stacks that contain the source code, but it does not display the information read from the standard line.
Bltin-exception displays all of the built-in exceptions and all the information.