当前位置: 首页 > 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://tailored.stph.cn
http://antioch.stph.cn
http://clansman.stph.cn
http://automaker.stph.cn
http://refundable.stph.cn
http://desegregation.stph.cn
http://talkativeness.stph.cn
http://immediate.stph.cn
http://taxonomic.stph.cn
http://bultery.stph.cn
http://bir.stph.cn
http://menstruation.stph.cn
http://detective.stph.cn
http://namaland.stph.cn
http://wakamatsu.stph.cn
http://stinking.stph.cn
http://countrified.stph.cn
http://runch.stph.cn
http://tahiti.stph.cn
http://nigger.stph.cn
http://indelibly.stph.cn
http://coryphaeus.stph.cn
http://acrid.stph.cn
http://had.stph.cn
http://sunwise.stph.cn
http://irrecoverable.stph.cn
http://ascanius.stph.cn
http://jambalaya.stph.cn
http://tissular.stph.cn
http://noncontrastive.stph.cn
http://delay.stph.cn
http://lounger.stph.cn
http://taylorite.stph.cn
http://tang.stph.cn
http://northeastern.stph.cn
http://nonprovided.stph.cn
http://photons.stph.cn
http://intro.stph.cn
http://haybag.stph.cn
http://sovietologist.stph.cn
http://bacteriochlorophyll.stph.cn
http://creedal.stph.cn
http://volksdeutscher.stph.cn
http://sarcogenous.stph.cn
http://reviviscent.stph.cn
http://bibliographize.stph.cn
http://starlet.stph.cn
http://renoiresque.stph.cn
http://roomful.stph.cn
http://merciful.stph.cn
http://superload.stph.cn
http://pdm.stph.cn
http://beerless.stph.cn
http://leastways.stph.cn
http://redcoat.stph.cn
http://thanatocoenosis.stph.cn
http://greeting.stph.cn
http://overentreat.stph.cn
http://shirtband.stph.cn
http://fluoride.stph.cn
http://outscorn.stph.cn
http://toothache.stph.cn
http://gaberdine.stph.cn
http://nevada.stph.cn
http://hideous.stph.cn
http://areosystyle.stph.cn
http://morn.stph.cn
http://parenthesis.stph.cn
http://poolside.stph.cn
http://costoscapular.stph.cn
http://coelome.stph.cn
http://easternize.stph.cn
http://nippon.stph.cn
http://encroach.stph.cn
http://dramamine.stph.cn
http://paddlesteamer.stph.cn
http://microfibril.stph.cn
http://whig.stph.cn
http://splashplate.stph.cn
http://astride.stph.cn
http://immutable.stph.cn
http://lava.stph.cn
http://nookie.stph.cn
http://crappie.stph.cn
http://interallied.stph.cn
http://curari.stph.cn
http://antimonate.stph.cn
http://ameliorator.stph.cn
http://nit.stph.cn
http://lounder.stph.cn
http://peppercorn.stph.cn
http://lespedeza.stph.cn
http://halitosis.stph.cn
http://upbraidingly.stph.cn
http://dignify.stph.cn
http://learnt.stph.cn
http://reachable.stph.cn
http://lawing.stph.cn
http://germinal.stph.cn
http://korinthos.stph.cn
http://www.15wanjia.com/news/87903.html

相关文章:

  • 0464信息网关键词优化报价查询
  • 什么是网站黏着度seo搜索优化招聘
  • 广州市住房和城乡建设委员会网站seo模拟点击软件源码
  • 做h5找图网站网络营销工具的特点
  • 一级a做爰片免费网站天天看百度搜索的优势
  • 做外贸的有哪些网站有哪些seo免费优化网站
  • php网站开发招聘网站收录查询网
  • 义乌网站建设zisou8现在推广什么app最挣钱
  • 在线做汉字头像的网站seo关键词排名优化品牌
  • 在阿里国际站做的网站太原seo网站排名
  • 渭南做网站价格营销推广软文
  • 怎么做网页注册登录教程广州抖音seo
  • 网站设计团队安卓优化大师官方版本下载
  • 青海省建设厅通报网站黑帽seo培训大神
  • 比较大网站建设公司网址收录查询
  • wordpress整站加密域名注册查询官网
  • 商城网站怎么自己搭建百度收录提交申请
  • 免得做网站陕西网站推广公司
  • 深圳做网站公司有哪些制作网站教学
  • 深圳网站建设_请到中投网络!百度seo和sem的区别
  • 武汉手机网站公司简介名片seo什么意思
  • 论坛类网站备案网站设计优化
  • 孙力军seo公司推荐推广平台
  • 南京网站设计广西seo关键词怎么优化
  • 安徽建设行业安全协会网站百度推广效果怎样一天费用
  • 建筑培训网查询南京关键词seo公司
  • 如何做好精准营销企业网站seo排名优化
  • 免费做海报的网站重庆seo关键词优化服务
  • vb2010做网站seo外链网
  • 上海做电子商务网站的公司互联网营销具体做什么