Python用法速查@异常处理

本文详细介绍了Python中的异常处理机制,包括常见的异常类型如ZeroDivisionError和TypeError,以及如何使用try...except...等结构来捕获和处理这些异常。此外,还展示了如何自定义异常类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JFLEARN

优快云这么拉会有人打赏?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值