Python 简明教程
Python - Nested try Block
Nested try Block in Python
在一个Python程序中,如果另一个 try-except 构造位于 try 块或其 except 块内,则称为嵌套try块。当不同的块(如外部块和内部块)可能导致不同的错误时,就需要这样做。为了处理它们,我们需要嵌套的try块。
In a Python program, if there is another try-except construct either inside either a try block or inside its except block, it is known as a nested-try block. This is needed when different blocks like outer and inner may cause different errors. To handle them, we need nested try blocks.
我们从一个具有单个“try - except- finally”构造的示例开始。如果try内部的语句遇到异常,它将由except块处理。无论是否发生异常,finally块总是执行。
We start with an example having a single "try − except − finally" construct. If the statements inside try encounter exception, it is handled by except block. With or without exception occurred, the finally block is always executed.
Example 1
在这里, try 块有“除以 0”的情况,因此 except 块派上用场了。它配备了一致处理带有 Exception 类的通用异常。
Here, the try block has "division by 0" situation, hence the except block comes into play. It is equipped to handle the generic exception with Exception class.
a=10
b=0
try:
print (a/b)
except Exception:
print ("General Exception")
finally:
print ("inside outer finally block")
它将生成以下 output −
It will produce the following output −
General Exception
inside outer finally block
Example 2
我们现在来看如何嵌套 try 结构。我们在现有的 try 块中放置另一个“try − except − finally”块。内部 try 的 except 关键词现在处理通用异常,而我们要求外部 try 的 except 块处理 ZeroDivisionError。
Let us now see how to nest the try constructs. We put another "try − except − finally" blocks inside the existing try block. The except keyword for inner try now handles generic Exception, while we ask the except block of outer try to handle ZeroDivisionError.
因为 try 块中没有发生异常,所以不会调用其对应的通用 Except。由外部 except 从句处理除以 0 的情况。
Since exception doesn’t occur in the inner try block, its corresponding generic Except isn’t called. The division by 0 situation is handled by outer except clause.
a=10
b=0
try:
print (a/b)
try:
print ("This is inner try block")
except Exception:
print ("General exception")
finally:
print ("inside inner finally block")
except ZeroDivisionError:
print ("Division by 0")
finally:
print ("inside outer finally block")
它将生成以下 output −
It will produce the following output −
Division by 0
inside outer finally block
Example 3
现在,我们逆转这种情况。在嵌套的 try 块之外,外部块没有任何异常引发,但是导致除以 0 的语句在内部 try 内,因此由内部 except 块处理异常。显然,将不会调用对应于外部 try 的 except 部分。
Now we reverse the situation. Out of the nested try blocks, the outer one doesn’t have any exception raised, but the statement causing division by 0 is inside inner try, and hence the exception handled by inner except block. Obviously, the except part corresponding to outer try: will not be called upon.
a=10
b=0
try:
print ("This is outer try block")
try:
print (a/b)
except ZeroDivisionError:
print ("Division by 0")
finally:
print ("inside inner finally block")
except Exception:
print ("General Exception")
finally:
print ("inside outer finally block")
它将生成以下 output −
It will produce the following output −
This is outer try block
Division by 0
inside inner finally block
inside outer finally block
最后,我们来讨论在嵌套块中可能发生的另一种情况。虽然在外部 try 中没有异常,但是没有合适的 except 块来处理内部 try 中的异常。
In the end, let us discuss another situation which may occur in case of nested blocks. While there isn’t any exception in the outer try:, there isn’t a suitable except block to handle the one inside the inner try: block.
Example 4
在以下示例中,内部 try 面临“除以 0”,但其对应的 except: 寻找 KeyError 而不是 ZeroDivisionError。因此,异常对象会传递给后续 except 语句的 except: 块,该 except 语句与外部 try: 语句相匹配。在那里,zeroDivisionError 异常被捕获并处理。
In the following example, the inner try: faces "division by 0", but its corresponding except: is looking for KeyError instead of ZeroDivisionError. Hence, the exception object is passed on to the except: block of the subsequent except statement matching with outer try: statement. There, the zeroDivisionError exception is trapped and handled.
a=10
b=0
try:
print ("This is outer try block")
try:
print (a/b)
except KeyError:
print ("Key Error")
finally:
print ("inside inner finally block")
except ZeroDivisionError:
print ("Division by 0")
finally:
print ("inside outer finally block")
它将生成以下 output −
It will produce the following output −
This is outer try block
inside inner finally block
Division by 0
inside outer finally block