Python用法速查@异常处理

Python 中的所有异常类(如ZeroDivisionError)都是从BaseException类派生的

>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

常见捕获方式

try...except...
try...except...else...
try...except...else...finally...
try...except...except...else...finally...
try...finally...

try...except...

try:
    k=1/0
except ZeroDivisionError as e: #捕获除零异常
    print(e)

print('Finish.')

#结果

D:\python\python.exe E:/PYTHON/Basics/Fun/HelloYoutube.py
division by zero
Finish.

Process finished with exit code 0

try...except.../except.../...

try:
    x = input('Enter x: ')
    y = input('Enter y: ')
    print x / y
except ZeroDivisionError as e:      # 处理 ZeroDivisionError 异常
    print 'ZeroDivisionError:',e
except TypeError as e:              # 处理 TypeError 异常 如 1 / 'a'
    print 'TypeError:',e

print 'hello world'

# 结果
Enter x: 3
Enter y: 'a'
TypeError: unsupported operand type(s) for /: 'int' and 'str'
hello world


except BaseException as e:...

捕获未知异常

try:
    x = input('Enter x: ')
    y = input('Enter y: ')
    print x / y
except ZeroDivisionError as e:      # 捕获 ZeroDivisionError 异常
    print 'ZeroDivisionError:',e
except TypeError as e:              # 捕获 TypeError 异常
    print 'TypeError:',e
except BaseException as e:          # 捕获未知异常 如 x被赋值一个回车
    print 'BaseException:',e

print 'hello world'

try...except...else...

try:
    x = input('Enter x: ')
    y = input('Enter y: ')
    print x / y
except ZeroDivisionError as e:
    print 'ZeroDivisionError:',e
except TypeError as e:
    print 'TypeError:',e
except BaseException as e:
    print 'BaseException:',e
else:
    print 'no error!'

print 'hello world'
# 结果
Enter x: 6
Enter y: 2
3
no error!
hello world

try...else...finally...

try:
    x = 1/0
    print x
finally:
    print 'DONE'
#结果

DONE
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero


raise

try:
    x = input('Enter x: ')
    y = input('Enter y: ')
    print x / y
except ZeroDivisionError as e:
    print 'ZeroDivisionError:',e
except TypeError as e:
    print 'TypeError:',e
except BaseException as e:
    print 'BaseException:',e
    raise                           # 使用 raise 抛出异常
else:
    print 'no error!'

print 'hello world'

#结果

Enter x:    # 这里输入回车键
BaseException: unexpected EOF while parsing (<string>, line 0)
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing


class Exception(n)

# 自定义异常类
class SomeError(Exception):
    pass

try:
    x = input('Enter x: ')
    y = input('Enter y: ')
    print x / y
except ZeroDivisionError as e:
    print 'ZeroDivisionError:',e
except TypeError as e:
    print 'TypeError:',e
except BaseException as e:
    print 'BaseException:',e
    raise SomeError('invalid value')    # 抛出自定义的异常
else:
    print 'no error!'

print 'hello world'
#结果
Enter x: 
BaseException: unexpected EOF while parsing (<string>, line 0)
----------------------------------------------------------------------
SomeError                            Traceback (most recent call last)
<ipython-input-20-66060b472f91> in <module>()
     12 except BaseException as e:
     13     print 'BaseException:',e
---> 14     raise SomeError('invalid value')
     15 else:
     16     print 'no error!'

SomeError: invalid value

参考

explore-python/Exceptioon

posted @ 2021-08-03 13:03  HUGBOY  阅读(69)  评论(0编辑  收藏  举报