An exception handling routine when debugging Python

Two basic approaches exception handling: using the try and except

The basic use of exception handling:

try:
	<语句块1>
except:
	<语句块2>

  Roughly logic is that if the abnormality occurs statement blocks 1, 2 block of statements is executed; if the abnormality is not a sentence block, a block of statements is executed, and skipping a block of statements 2.
To distinguish between different types of anomalies:

try:
<语句块1>
except <异常类型><语句块2>

Only when this type occurs, it will execute a block of statements 2

try:
    num = eval(input("请输入一个整数:"))
    print(num**2)
except :
    print("输入的不是整数")

Specifies the exception type

try:
    num = eval(input("请输入一个整数:"))
    print(num**2)
except NameError:
    print("输入的不是整数")
Published 37 original articles · won praise 17 · views 2590

Guess you like

Origin blog.csdn.net/qq_44384577/article/details/105294262