try: except 语句

2015-12-15  本文已影响23人  Rim99
try:
    <try suite>                      # always executed
except <exception class> as <name>:  # <exception class> can be subclass of Exception Class
    <except suite>                   # excecuted when excecuting <try suite> meets errors

栗子

>>> try:
        x = 1/0
    except ZeroDivisionError as e:
        print('handling a', type(e))
        x = 0
handling a <class 'ZeroDivisionError'>
>>> x
0

上一篇下一篇

猜你喜欢

热点阅读