This article brings you an introduction to python errors, exceptions and program debugging methods (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Exceptions are errors caused by Python programs during running. If an unhandled exception is thrown in the program, the program will terminate due to the exception. Only by adding exception handling to the program can the program be more " robust".
Python has its own grammatical form for handling exceptions. Master how to handle exceptions and program debugging in Python. The main contents are:
1. Spelling errors
refer to misspelling of keywords in the Python language, misspelling of variable names, function names, etc.
If the keyword is spelled incorrectly, a SyntaxError (syntax error)
will be prompted, and if the variable name or function name is spelled incorrectly, a NameError error
will be prompted at runtime.
2. The script program does not comply with Python's syntax specifications
For example, there are missing brackets, colons and other symbols, and expressions are written incorrectly.
3. Indentation error
Because Python grammar stipulates that indentation is one of the program's grammar, which should be a unique aspect of the Python language. Generally speaking, Python's standard indentation is 4 spaces. Of course, you can use Tab according to your own habits. However, the same indentation style should be used consistently in the same program or project.
Exceptions are errors caused by Python programs during running. If an unhandled exception is thrown in the program, the script will terminate due to the exception. Only by catching these exceptions in the program and performing relevant processing can the program not be interrupted.
The try statement is used in Python to handle exceptions. Like other statements in Python, the try statement also uses an indentation structure. The try statement also has an optional Selected else statement block. The basic form of a general try statement is as follows.
try: <语句(块)> #可能产生异常的语句(块) except <异常名1>: #要处理的异常 <语句(块)> #异常处理语句 except <异常名2>: #要处理的异常 <语句(块)> #异常处理语句 else: <语句(块)> #未触发异常,则执行该语句(块) finally: <语句(块)> #始终执行该语,一般为了达到释放资源等目的
In actual applications, some statements can be used according to the needs of the program. Common forms are:
Form 1:
try: <语句(块)> except <异常名1>: <语句(块)>
Example:
def testTry (index, flag=False): stulst = ["John","Jenny","Tom"] if flag: #flag为True时,捕获异常 try: astu = stulst [index] except IndexError: print("IndexError") return "Try Test Finished!" else: #flag为False时,不捕获异常 astu =stulst [index] return "No Try Test Finished!" print("Right params testing start...") print (testTry (1, True)) #不越界参数,捕获异常(正常) print (testTry (1, False)) #不越界参数,不捕获异常(正常) print("Error params testing start...") print (testTry (4, True)) #越界参数,捕获异常(正常) print (testTry (4, False)) #越界参数,不捕获异常(程序运行会中断)
Form 2:
try: <语句(块)> except < 异常名1>: <语句(块)> finally: <语句(块)>
Example:
def testTryFinally (index): stulst = ["John","Jenny", "Tom"] af = open ("my.txt", 'wt+') try: af.write(stulst[index]) except: pass finally: af.close() #无论是否产生越界异常,都关闭文件 print("File already had been closed!") print('No IndexError...') testTryFinally (1) #无越界异常,正常关闭文件 print('IndexError...') testTryFinally (4) #有越界异常,正常关闭文件
Common exceptions in Python have been predefined. In an interactive environment, using the dir (__builtins__) command will display all predefined exceptions.
Exception name | Description |
---|---|
Call Exception raised by non-existent method | |
Exception raised by encountering the end of file | |
Exception caused by import module error | |
Exception caused by list out of bounds | |
Exceptions caused by I/O operations, such as errors in opening files, etc. | |
Exceptions caused by using keywords that do not exist in the dictionary | |
Exception caused by using a non-existent variable name | |
Exception caused by incorrect statement block indentation | |
Exception raised by a value that does not exist in the search list | |
The divisor is Exception raised by zero |
The above is the detailed content of Introduction to python errors, exceptions and program debugging methods (with code). For more information, please follow other related articles on the PHP Chinese website!