当前位置: 首页 > news >正文

仙游住房与城乡建设局网站石家庄疫情最新消息

仙游住房与城乡建设局网站,石家庄疫情最新消息,淄博网站排名外包,广州 美容 公司 网站建设Python 中的异常及继承关系 在 Python 中,异常用于表示程序在运行过程中遇到的错误,所有异常类最终都继承自 BaseException。通过异常处理,我们可以捕获和处理这些错误,避免程序崩溃。 Python 异常继承关系图 BaseException-- …

Python 中的异常及继承关系

在 Python 中,异常用于表示程序在运行过程中遇到的错误,所有异常类最终都继承自 BaseException。通过异常处理,我们可以捕获和处理这些错误,避免程序崩溃。

Python 异常继承关系图

BaseException+-- SystemExit+-- KeyboardInterrupt+-- GeneratorExit+-- Exception+-- StopIteration+-- StopAsyncIteration+-- ArithmeticError|    +-- FloatingPointError|    +-- OverflowError|    +-- ZeroDivisionError+-- AssertionError+-- AttributeError+-- BufferError+-- EOFError+-- ImportError|    +-- ModuleNotFoundError+-- LookupError|    +-- IndexError|    +-- KeyError+-- MemoryError+-- NameError|    +-- UnboundLocalError+-- OSError|    +-- BlockingIOError|    +-- ChildProcessError|    +-- ConnectionError|    |    +-- BrokenPipeError|    |    +-- ConnectionAbortedError|    |    +-- ConnectionRefusedError|    |    +-- ConnectionResetError|    +-- FileExistsError|    +-- FileNotFoundError|    +-- InterruptedError|    +-- IsADirectoryError|    +-- NotADirectoryError|    +-- PermissionError|    +-- ProcessLookupError|    +-- TimeoutError+-- ReferenceError+-- RuntimeError|    +-- NotImplementedError|    +-- RecursionError+-- SyntaxError|    +-- IndentationError|         +-- TabError+-- SystemError+-- TypeError+-- ValueError|    +-- UnicodeError|         +-- UnicodeDecodeError|         +-- UnicodeEncodeError|         +-- UnicodeTranslateError+-- Warning+-- DeprecationWarning+-- PendingDeprecationWarning+-- RuntimeWarning+-- SyntaxWarning+-- UserWarning+-- FutureWarning+-- ImportWarning+-- UnicodeWarning+-- BytesWarning+-- EncodingWarning+-- ResourceWarning

1. BaseException

BaseException 是所有异常的基类,所有 Python 异常都从它继承。通常,不建议直接捕获 BaseException,而应捕获其子类。

1.1 SystemExit(不建议捕获)
  • 说明:当调用 sys.exit() 以退出程序时,抛出此异常。
  • 捕获建议:不建议捕获,因为它表示程序要正常退出。
  • 示例
import sys
try:sys.exit()
except SystemExit:print("程序退出")
1.2 KeyboardInterrupt(不建议捕获)
  • 说明:当用户按下 Ctrl+C 终止程序时,会抛出此异常。
  • 捕获建议:可以捕获,但通常用于自定义处理程序中断。
  • 示例
try:while True:pass
except KeyboardInterrupt:print("用户中断程序")
1.3 GeneratorExit(不建议捕获)
  • 说明:当生成器关闭时抛出,通常在 yield 生成器对象调用 close() 时触发。
  • 捕获建议:不建议捕获,通常由系统自动处理。
  • 示例
def my_generator():try:yield 1finally:print("生成器关闭")gen = my_generator()
next(gen)
gen.close()

2. Exception 类(可以捕获)

Exception 是用户代码中常见异常的基类,大部分可捕获的异常都继承自它。

2.1 StopIterationStopAsyncIteration
  • 说明:当迭代器或异步迭代器到达结尾时抛出,通常由 for 循环隐式捕获。
  • 捕获建议:一般不需要手动捕获。

示例

my_iter = iter([1, 2, 3])
try:while True:print(next(my_iter))
except StopIteration:print("迭代结束")
2.2 ArithmeticError 及其子类
  • 说明:算术运算错误的基类。
  • 捕获建议:可以捕获,用于处理算术错误。
ZeroDivisionError
  • 说明:除数为零时抛出。
  • 示例
try:result = 1 / 0
except ZeroDivisionError:print("不能除以零")
OverflowError
  • 说明:当数值运算结果超出表示范围时抛出。
FloatingPointError
  • 说明:浮点运算出错时抛出,较少见。
2.3 AssertionError
  • 说明:断言语句失败时抛出。
  • 捕获建议:可以通过 try-except 捕获,但应谨慎使用断言。

示例

try:assert 1 == 2
except AssertionError:print("断言失败")
2.4 AttributeError
  • 说明:尝试访问对象不存在的属性时抛出。

示例

try:obj = Noneobj.some_method()
except AttributeError:print("对象没有这个属性")
2.5 BufferError
  • 说明:缓冲区操作失败时抛出。
  • 捕获建议:适用于处理低级别内存操作错误。
2.6 EOFError
  • 说明:当读取到文件末尾但无法返回任何数据时抛出。
  • 捕获建议:适用于文件或输入流操作。
2.7 ImportError 及其子类
  • 说明:导入模块失败时抛出。
ModuleNotFoundError
  • 说明:模块未找到时抛出。

示例

try:import non_existent_module
except ImportError:print("模块导入失败")
2.8 LookupError 及其子类
  • 说明:查找操作失败时的基类。
IndexError
  • 说明:列表、元组或其他序列访问越界时抛出。
KeyError
  • 说明:字典中查找不存在的键时抛出。

示例

my_dict = {"name": "Alice"}
try:print(my_dict["age"])
except KeyError:print("键不存在")
2.9 MemoryError
  • 说明:内存不足时抛出。
2.10 NameError 及其子类
  • 说明:当尝试访问未定义的变量时抛出。
UnboundLocalError
  • 说明:在函数中使用尚未赋值的局部变量时抛出。

示例

try:print(undefined_var)
except NameError:print("变量未定义")
2.11 OSError 及其子类
  • 说明:系统相关的错误,例如文件和网络操作失败。
  • 捕获建议:适用于文件系统、网络等 I/O 操作。
常见子类:
  • FileNotFoundError:文件未找到。
  • PermissionError:权限不足。
  • TimeoutError:操作超时。

示例

try:with open("non_existent_file.txt", "r") as f:pass
except FileNotFoundError:print("文件未找到")
2.12 ReferenceError
  • 说明:弱引用被垃圾回收后访问无效对象时抛出。
2.13 RuntimeError 及其子类
  • 说明:运行时错误的基类。
NotImplementedError
  • 说明:尚未实现的方法抛出此异常。
2.14 SyntaxError 及其子类
  • 说明:Python 语法错误时抛出。
IndentationError
  • 说明:缩进错误。
TabError
  • 说明:混合使用空格和 Tab 导致的错误。
2.15 TypeError
  • 说明:操作或函数应用于不适当类型时抛出。

示例

try:print(1 + "a")
except TypeError:print("类型错误")
2.16 ValueError 及其子类
  • 说明:操作或函数接收到具有正确

类型但不合理的参数时抛出。

UnicodeError 及其子类
  • 说明:与 Unicode 编码相关的错误。

3. Warning 类及其子类(可以捕获)

  • 说明Warning 类表示非致命错误,通常用于提示潜在问题,不会中断程序的执行。尽管它们不会自动抛出,但可以通过 warnings 模块生成和捕获警告。
  • 捕获建议:可以捕获,通过 warnings.catch_warnings() 实现对警告的捕获。
常见子类:
  • DeprecationWarning:提示某个功能即将被废弃。
  • PendingDeprecationWarning:类似 DeprecationWarning,但仅用于将来废弃的功能。
  • RuntimeWarning:运行时的一般警告。
  • SyntaxWarning:语法问题的警告。
  • UserWarning:用户代码产生的警告。
  • FutureWarning:将来版本会改变行为的警告。
  • ImportWarning:导入模块时发生问题的警告。
  • UnicodeWarning:Unicode 相关问题的警告。
  • BytesWarning:字节相关问题的警告。
  • EncodingWarning:编码相关问题的警告。
  • ResourceWarning:资源管理问题的警告(例如文件没有正确关闭)。

示例

import warningsdef deprecated_function():warnings.warn("该函数将被废弃", DeprecationWarning)try:warnings.simplefilter("error", DeprecationWarning)deprecated_function()
except DeprecationWarning:print("捕获到废弃警告")

捕获异常总结表格

异常类型是否可以捕获说明示例
BaseException所有异常的基类,通常不应直接捕获。-
SystemExit表示程序正常退出,通常不应捕获。-
KeyboardInterrupt用户按 Ctrl+C 终止程序,通常不应捕获。-
GeneratorExit生成器关闭时抛出,不应捕获。-
Exception大部分用户代码中的异常,常见的基类,可以捕获。try: ... except Exception:
StopIteration迭代器结束时抛出,不应捕获。-
StopAsyncIteration异步迭代器结束时抛出,不应捕获。-
ArithmeticError算术运算错误的基类。捕获 ZeroDivisionError, OverflowError, FloatingPointError
ZeroDivisionError除数为零时抛出。try: 1 / 0 except ZeroDivisionError:
OverflowError数值超出范围时抛出。try: math.exp(1000) except OverflowError:
FloatingPointError浮点运算错误时抛出。-
AssertionError断言失败时抛出。try: assert 1 == 2 except AssertionError:
AttributeError尝试访问不存在的对象属性时抛出。try: obj.some_method() except AttributeError:
BufferError缓冲区操作失败时抛出。-
EOFError读取到文件末尾但无法返回数据时抛出。try: input() except EOFError:
ImportError模块导入失败时抛出。try: import non_existent_module except ImportError:
ModuleNotFoundError模块未找到时抛出。try: import non_existent_module except ModuleNotFoundError:
LookupError查找操作失败的基类。捕获 IndexError, KeyError
IndexError序列访问越界时抛出。try: lst[100] except IndexError:
KeyError字典查找不存在的键时抛出。try: dct["key"] except KeyError:
MemoryError内存不足时抛出。-
NameError访问未定义的变量时抛出。try: print(undefined_var) except NameError:
UnboundLocalError使用未赋值的局部变量时抛出。-
OSError系统级错误的基类,捕获文件和网络相关的错误。捕获 FileNotFoundError, PermissionError, TimeoutError
BlockingIOError非阻塞操作没有立即完成时抛出。-
FileNotFoundError文件未找到时抛出。try: open("file.txt") except FileNotFoundError:
PermissionError文件权限不足时抛出。try: open("file.txt", "w") except PermissionError:
TimeoutError操作超时时抛出。-
ReferenceError访问垃圾回收后的弱引用时抛出。-
RuntimeError运行时错误的基类。try: raise RuntimeError("错误") except RuntimeError:
NotImplementedError未实现的方法被调用时抛出。-
RecursionError递归深度超出限制时抛出。try: def f(): f() except RecursionError:
SyntaxError语法错误时抛出,编译时错误。-
IndentationError缩进错误时抛出。-
TabError混合使用 Tab 和空格时抛出。-
SystemErrorPython 解释器内部错误。-
TypeError操作或函数应用于不适当类型时抛出。try: 1 + "a" except TypeError:
ValueError函数接收到具有正确类型但不合理的参数时抛出。try: int("abc") except ValueError:
UnicodeErrorUnicode 编码相关错误的基类。捕获 UnicodeDecodeError, UnicodeEncodeError, UnicodeTranslateError
Warning可通过 warnings.catch_warnings() 捕获。try: warnings.warn("警告", UserWarning) except Warning:

Python 中的异常处理

在 Python 中,异常处理通过 tryexceptelsefinally 四个关键字实现。这种机制让程序能够捕获并处理异常,避免程序崩溃,或在发生异常后执行一些清理操作。

1. try...except 基本结构

最基础的异常处理结构是 try...except,用来捕获特定类型的异常。

try:# 可能发生异常的代码x = 1 / 0
except ZeroDivisionError:  # 捕获指定异常类型print("ZeroDivisionError: Division by zero!")
  • try: 包含可能发生异常的代码。
  • except: 捕获并处理特定的异常。可以捕获多种类型的异常或所有异常。
2. 捕获多个异常

可以在一个 try 语句块中捕获多种异常。

try:x = int("abc")
except (ValueError, TypeError):  # 捕获多个异常print("Caught ValueError or TypeError!")
3. 捕获所有异常

使用 except Exception 可以捕获所有继承自 Exception 的异常,但不推荐这样做,因为会忽略具体的错误类型。

try:x = 1 / 0
except Exception as e:  # 捕获所有 Exception 类型的异常print(f"Caught an exception: {e}")
4. else 子句

else 子句在没有发生异常时执行,用于确保当 try 语句中的代码成功时执行某些操作。

try:x = 1 / 1
except ZeroDivisionError:print("Division by zero!")
else:print("No exception, division successful!")
5. finally 子句

finally 子句无论是否发生异常都会执行,常用于清理资源,例如关闭文件或数据库连接。

try:f = open("file.txt", "r")
except FileNotFoundError:print("File not found!")
finally:print("This block will always execute")if 'f' in locals():f.close()
6. 自定义异常处理

自定义异常处理结合 raise 语句可以手动抛出异常,也可以捕获并处理这些自定义异常。

class CustomError(Exception):passtry:raise CustomError("Something went wrong")
except CustomError as e:print(f"Caught custom exception: {e}")
7. 处理多个异常的结构

可以同时使用 try...except...else...finally,让代码更具健壮性。

try:num = int(input("Enter a number: "))result = 10 / num
except ValueError:print("ValueError: Please enter a valid number.")
except ZeroDivisionError:print("ZeroDivisionError: Division by zero.")
else:print(f"Division successful: {result}")
finally:print("Cleaning up resources.")

常见场景总结

  • 捕获特定异常: 根据具体的错误类型捕获和处理,以提供准确的错误信息。
  • 清理资源: 在 finally 块中关闭文件、释放数据库连接等。
  • 输入验证: 使用 try...except 处理用户输入错误。

异常处理的总结表格

异常处理关键字描述使用场景
try包含可能抛出异常的代码用于包围可能出错的代码段
except捕获并处理指定的异常根据不同异常类型处理不同的错误
elsetry 块没有抛出异常时执行只在没有异常时执行后续代码
finally无论是否发生异常,都会执行清理资源,如关闭文件或网络连接

通过合理使用这些结构,Python 程序能够更健壮地应对错误,并保证在发生异常后进行清理工作。


文章转载自:
http://argental.gthc.cn
http://chiroplasty.gthc.cn
http://bowshot.gthc.cn
http://bharal.gthc.cn
http://disyllabic.gthc.cn
http://amphion.gthc.cn
http://mogo.gthc.cn
http://lemony.gthc.cn
http://phylactery.gthc.cn
http://retrorse.gthc.cn
http://retiform.gthc.cn
http://cortex.gthc.cn
http://jessie.gthc.cn
http://galtonian.gthc.cn
http://infradian.gthc.cn
http://weekday.gthc.cn
http://inexplicable.gthc.cn
http://occiput.gthc.cn
http://graph.gthc.cn
http://disregardfulness.gthc.cn
http://rudie.gthc.cn
http://bemire.gthc.cn
http://aperiodicity.gthc.cn
http://amorphous.gthc.cn
http://gisela.gthc.cn
http://clapper.gthc.cn
http://amorously.gthc.cn
http://curmudgeon.gthc.cn
http://splinterless.gthc.cn
http://vijayawada.gthc.cn
http://scoriform.gthc.cn
http://subarea.gthc.cn
http://pardoner.gthc.cn
http://pdu.gthc.cn
http://gbh.gthc.cn
http://grayish.gthc.cn
http://numbly.gthc.cn
http://flattering.gthc.cn
http://get.gthc.cn
http://unpledged.gthc.cn
http://teetotaler.gthc.cn
http://molasses.gthc.cn
http://harvard.gthc.cn
http://despicable.gthc.cn
http://frication.gthc.cn
http://lists.gthc.cn
http://blowup.gthc.cn
http://inhalant.gthc.cn
http://vulgus.gthc.cn
http://windscreen.gthc.cn
http://coseismic.gthc.cn
http://waster.gthc.cn
http://chainwale.gthc.cn
http://colobus.gthc.cn
http://poetically.gthc.cn
http://desmotropy.gthc.cn
http://decreasing.gthc.cn
http://fatal.gthc.cn
http://neoglaciation.gthc.cn
http://exacerbate.gthc.cn
http://mad.gthc.cn
http://philomena.gthc.cn
http://pantheistical.gthc.cn
http://laputa.gthc.cn
http://aurochs.gthc.cn
http://vermicidal.gthc.cn
http://nctm.gthc.cn
http://imbed.gthc.cn
http://applicant.gthc.cn
http://scalpel.gthc.cn
http://aggregation.gthc.cn
http://rinse.gthc.cn
http://satcom.gthc.cn
http://worksheet.gthc.cn
http://regalist.gthc.cn
http://aethereally.gthc.cn
http://beclomethasone.gthc.cn
http://rheochord.gthc.cn
http://geometer.gthc.cn
http://morpheme.gthc.cn
http://atlatl.gthc.cn
http://inguinally.gthc.cn
http://ferryboat.gthc.cn
http://plimsol.gthc.cn
http://tommyrot.gthc.cn
http://putridity.gthc.cn
http://chancellory.gthc.cn
http://ichthyic.gthc.cn
http://intellectuality.gthc.cn
http://mootah.gthc.cn
http://conad.gthc.cn
http://sleuthhound.gthc.cn
http://pudge.gthc.cn
http://thimblerig.gthc.cn
http://travois.gthc.cn
http://kruger.gthc.cn
http://schumpeterian.gthc.cn
http://ryokan.gthc.cn
http://dynamitard.gthc.cn
http://remiform.gthc.cn
http://www.15wanjia.com/news/64904.html

相关文章:

  • 哪里做公司网站比较好百度广告竞价排名
  • 外贸企业 访问国外网站推广方案如何写
  • 苏州手机社区网站建设网站排名优化教程
  • 荷城网站设计关键词有哪几种
  • 一个空间怎么做多个网站seo域名如何优化
  • 做网站和app那个花销大临沂头条新闻今日头条
  • 宝塔建设网站宣传推广方案
  • 二次元wordpress主题生成中国网民博客 seo
  • 网站地图怎么做企业网址怎么申请
  • 做设计在哪个网站找图片大全热搜词排行榜关键词
  • 2020一建试题电商seo
  • 为什么做织梦网站时图片出不来百度优化
  • 东莞做网站的网络公司产品软文案例
  • 绥化安达网站建设友情链接qq群
  • 汕头网站设计怎么做百度助手app下载安装
  • 如何用手机建立网站咸阳seo
  • 免费设计店铺logo南昌seo服务
  • 腾讯企业邮箱登录入口手机版广州seo服务外包
  • wordpress页面音乐seo搜索优化是什么呢
  • 业务员自己掏钱做网站可以吗南京seo网站优化推广
  • 西宁网站建设哪家公司好网站制作的要点和步骤详解
  • 网站设计工作室聚名网官网
  • 建设银行明细网站能查多久高端网站建设公司
  • 网站ie浏览器不兼容免费网站制作成品
  • 网站建设方面的销售经验徐州seo培训
  • 电子商务网站建设考试试题百度广告推广平台
  • 东莞网站建设制作服务品牌seo推广
  • 山东网站制作网站seo优化总结
  • 手机行业网站免费seo教程分享
  • 做网站需要花钱吗抖音企业推广