Python学习笔记8:异常

本文介绍了Python中的异常处理机制,包括内置异常类如ZeroDivisionError和自定义异常的创建。通过try/except语句演示了如何捕获和处理异常,同时讲解了else和finally子句的用途。此外,还提到了警告系统以及如何使用filterwarnings来管理警告。

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

异常

一些内置的异常类

类名描述
Exception几乎所有的异常类都是从它派生而来的
AttributeError引用属性或给它赋值失败时引发
OSError操作系统不能执行指定的任务(如打开文件)时引发,有多个子类
IndexError使用序列中不存在的索引时引发,为LookupError的子类
KeyError使用映射中不存在的键时引发,为LookupError的子类
NameError找不到名称(变量)时引发
SyntaxError代码不正确时引发
TypeError将内置操作或函数用于类型不正确的对象时引发
ValueError将内置操作或函数用于这样的对象时引发:其类型正确但包含的值不合适
ZeroDivisionError在除法或求模运算的第二个参数为零时引发

自定义异常类

# 务必直接或间接地继承Exception(这意味着从任何内置异常类派生都可以)
class SomeCustomException(Exception): pass

捕获异常

try/except语句

try: 
    x = int(input('Enter the first number: ')) 
    y = int(input('Enter the second number: ')) 
    print(x / y) 
except ZeroDivisionError: 
    print("The second number can't be zero!")

向上传播异常 raise

# 通过muffled,启用了“抑制”功能
class MuffledCalculator: 
    muffled = False 
    def calc(self, expr): 
        try: 
            return eval(expr) 
        except ZeroDivisionError: 
            if self.muffled: 
                print('Division by zero is illegal') 
            else: 
                raise
>>>try:
...    1/0
...except ZeroDivisionError:
...    raise ValueError

Traceback (most recent call last):
  File "<pyshell#4>", line 2, in <module>
    1/0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#4>", line 4, in <module>
    raise ValueError
ValueError

捕获多个异常

try: 
    x = int(input('Enter the first number: ')) 
    y = int(input('Enter the second number: ')) 
    print(x / y) 
except (ZeroDivisionError, TypeError, NameError): 
    print('Your numbers were bogus ...')

访问异常对象本身

# 只提供一个参数:一个元组
try: 
    x = int(input('Enter the first number: ')) 
    y = int(input('Enter the second number: ')) 
    print(x / y) 
except (ZeroDivisionError, TypeError) as e: 
    print(e)

捕获所有异常

# 这还将捕获用户使用Ctrl + C终止执行的企图、调用函数sys.exit来终止执行的企图等
# 在大多数情况下,更好的选择是使用except Exception as e并对异常对象进行检查
# 这样做将让不是从Exception派生而来的为数不多的异常成为漏网之鱼,其中包括SystemExit和KeyboardInterrupt,因为它们是从BaseException(Exception的超类)派生而来的

try: 
    x = int(input('Enter the first number: ')) 
    y = int(input('Enter the second number: ')) 
    print(x / y) 
except: 
    print('Something wrong happened ...')

else子句 正常处理后

try: 
    print('A simple task') 
except: 
    print('What? Something went wrong?') 
else: 
    print('Ah ... It went as planned.')

 # 输出如下:
A simple task 
Ah ... It went as planned.

finally子句 用于发生异常时执行清理工作

try: 
    1 / 0 
except NameError: 
    print("Unknown variable") 
else: 
    print("That went well!") 
finally: 
    print("Cleaning up.")

警告

>>> from warnings import warn 
>>> warn("I've got a bad feeling about this.") 
__main__:1: UserWarning: I've got a bad feeling about this. 
>>>

警告只显示一次。如果再次运行最后一行代码,什么事情都不会发生。

filterwarnings来抑制你发出的警告,并指定要采取的措施,如"error"或"ignore"

>>> from warnings import filterwarnings 
>>> filterwarnings("ignore") 
>>> warn("Anyone out there?") 
>>> filterwarnings("error") 
>>> warn("Something is very wrong!") 
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module> 
UserWarning: Something is very wrong!

小结

异常对象:异常情况(如发生错误)是用异常对象表示的。对于异常情况,有多种处理方式;如果忽略,将导致程序终止。

引发异常:可使用raise语句来引发异常。它将一个异常类或异常实例作为参数,但你也可提供两个参数(异常和错误消息)。如果在except子句中调用raise时没有提供任何参数,它将重新引发该子句捕获的异常。

自定义的异常类:你可通过从Exception派生来创建自定义的异常。

捕获异常:要捕获异常,可在try语句中使用except子句。在except子句中,如果没有指定异常类,将捕获所有的异常。你可指定多个异常类,方法是将它们放在元组中。如果向except提供两个参数,第二个参数将关联到异常对象。在同一条try/except语句中,可包含多个except子句,以便对不同的异常采取不同的措施。

else子句:除except子句外,你还可使用else子句,它在主try块没有引发异常时执行。

finally:要确保代码块(如清理代码)无论是否引发异常都将执行,可使用try/finally,并将代码块放在finally子句中。

异常和函数:在函数中引发异常时,异常将传播到调用函数的地方(对方法来说亦如此)。

警告:警告类似于异常,但(通常)只打印一条错误消息。你可指定警告类别,它们是Warning的子类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值