try:
suite1
except exception1:
suite2
except (exception2,exception3):
suite3
except exception4 as reason:
suite4
except:
suite5
else:
suite5
finally:
suit6
注意:中间的 except,else,finally都是可选的,但至少有一个,
不然try就没有意义了,根据实际中的需求来选择。
所有错误类型都继承自 Exception
https://docs.python.org/3/library/exceptions.html
注意:如果抛出父类异常,在子类不会再获取,如下:
try:
fun()
except Exception as e:
raise Exception
except ImportError as e:
raise ImportError
finally:
pass
在上面的例子中,下面的 ImportError 就不会被抛出,
因为ImportError 继承 Exception,但是可以把 Exception 放在后面是可以的。
e 可以得到系统给出的报错信息。