Python - try- except 块


您还可以使用except语句,不定义异常,如下所示 -

try:
   You do your operations here
   ......................
except:
   If there is any exception, then execute this block.
   ......................
else:
   If there is no exception then execute this block.

这种 try-except 语句捕获所有发生的异常。不过,使用这种 try-except 语句并不被认为是一种好的编程实践,因为它捕获了所有异常,但并不能让程序员识别可能发生问题的根本原因。

您还可以使用相同的 except 语句来处理多个异常,如下所示 -

try:
   You do your operations here
   ......................
except(Exception1[, Exception2[,...ExceptionN]]]):
   If there is any exception from the given exception list,
   then execute this block.
   ......................
else:
   If there is no exception then execute this block.