当前位置:网站首页>第9章 异常try...except...else...finally
第9章 异常try...except...else...finally
2022-07-31 06:09:00 【MallocLu】
异常
Python使用异常对象来表示异常状态,并在遇到错误时引发异常。异常对象未被处理(或捕获)时,程序将终止并显示一条错误信息(traceback)。
>>> 1 / 0
Traceback (most recent call last):
File "<input>", line 1, in <module>
ZeroDivisionError: division by zero
异常包含内置异常和自定义异常2种(没有本质区别,内置异常即Python已经定义且经常使用的异常,自定义异常则多与实际开发的业务有关)
内置异常
仅常见的内置异常,其实还有很多
BaseException
+-- Exception
| +-- AttributeError 引用属性或给它赋值失败时引发
| +-- OSError 操作系统不能执行指定的任务时引发
| +-- IndexError 使用序列中不存在索引时引发
| +-- KeyError 使用映射中不存在键时引发
| +-- NameError 找不到名称(变量)时引发
| +-- SyntaxError 代码不正确时引发
| +-- TypeError 将内置操作或函数用于类型不正确的变量时引发
| +-- ValueError 将内置操作或函数用于这样的对象时引发:其类型正确但包含的值不合适
| +-- ZeroDivisionError 在除法或求模运算的第二个参数为零时引发
+-- KeyboardInterrupt
+-- SystemExit
自定义异常
class NewException(Exception):
def __init__(self, errorInfo):
super().__init__()
self.errorInfo = errorInfo
def __str__(self):
return self.errorInfo
抛出异常
1) raise:单独一个 raise。该语句引发当前上下文中捕获的异常(比如在 except 块中),或默认引发 RuntimeError 异常
注意:raise ... from None的用法
# 引发默认的RuntuimeError
raise
# Traceback (most recent call last):
# File "C:/Users/shang/Desktop/pythonProject/main.py", line 1, in <module>
# raise
# RuntimeError: No active exception to reraise
# 1 / 0 的位置引发了ZeroDivisionError,raise将整个异常栈抛出
try:
1 / 0
except ZeroDivisionError:
raise
# Traceback (most recent call last):
# File "C:/Users/shang/Desktop/pythonProject/main.py", line 2, in <module>
# 1 / 0
# ZeroDivisionError: division by zero
# 1 / 0 的位置引发了ZeroDivisionError,except ZeroDivisionError异常处理抛出了ValueError,except Exception通过raise将整个异常栈抛出
try:
try:
1 / 0
except ZeroDivisionError:
raise ValueError
except Exception:
raise
# Traceback (most recent call last):
# File "C:/Users/shang/Desktop/pythonProject/main.py", line 3, in <module>
# 1 / 0
# ZeroDivisionError: division by zero
#
# During handling of the above exception, another exception occurred:
#
# Traceback (most recent call last):
# File "C:/Users/shang/Desktop/pythonProject/main.py", line 5, in <module>
# raise ValueError
# ValueError
# 通过raise ... from None来禁用上下文,raise并没有抛出整个异常栈而是从ValueError开始向外的异常栈
try:
try:
1 / 0
except ZeroDivisionError:
raise ValueError from None
except Exception:
raise
# Traceback (most recent call last):
# File "C:/Users/shang/Desktop/pythonProject/main.py", line 5, in <module>
# raise ValueError from None
# ValueError
2) raise ZeroDivisionError
# Traceback (most recent call last):
# File "E:\anaconda\envs\py37tfcpu\lib\code.py", line 90, in runcode
# exec(code, self.locals)
# File "<input>", line 1, in <module>
# ZeroDivisionError
3) raise ZeroDivisionError("除数不能为0")
# Traceback (most recent call last):
# File "E:\anaconda\envs\py37tfcpu\lib\code.py", line 90, in runcode
# exec(code, self.locals)
# File "<input>", line 1, in <module>
# ZeroDivisionError: 除数不能为0
捕获异常
总体结构
# try块必选,except块&finally块二选一,else块可选
# try块中的代码出现异常,终止try块后面的代码执行转入except块
# try块中的代码未出现异常,执行完try块后转入else块
# try块中的代码无论是否出现异常,最后执行finally块
try:
pass
except:
pass
else:
pass
finally:
pass
except块
# 捕获所有异常,不建议使用,因为包含KeyboardInterrupt等
except:
# 捕获一种异常
except ZeroDivisionError:
print("The second number can't bu zero!")
# 捕获多种异常
except ZeroDivisionError:
print("The second number can't bu zero!")
except TypeError:
print("That wasn't a number, was it?")
# 捕获多种异常
except (ZeroDivisionError, TypeError):
print('Your numbers were bogus ...')
# 访问异常对象本身
except (ZeroDivisionError, TypeError) as e:
print(e)
处理异常
1)不处理,程序将异常终止
1 / 0
print('Hello World')
# Traceback (most recent call last):
# File "C:/Users/shang/Desktop/pythonProject/main.py", line 1, in <module>
# 1 / 0
# ZeroDivisionError: division by zero
#
# Process finished with exit code 1
2)忽略,程序继续执行后面的语句
try:
1 / 0
except Exception as e:
pass
print('Hello World')
# Hello World
#
# Process finished with exit code 0
3)处理,执行完except块的处理过程后,程序继续执行后面的语句
try:
1 / 0
except Exception as e:
print(e)
print('Hello World')
# division by zero
# Hello World
#
# Process finished with exit code 0
4)抛出,交由外层的调用者处理
try:
1 / 0
except Exception as e:
raise
print('Hello World')
警告
#使用warnings模块中的warn函数warn发出警告
>>>from warnings import warn
>>>warn("I've got a bad feeling about this.")
#警告只显示一次,如果再次运行最后一行代码,什么事情都不会发生
#使用warnings模块中的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 "<input>", line 1, in <module>
UserWarning: Something is very wrong!
#发出警告时,可以指定要引发的警告类别(必须是Warning的子类,默认UserWarning)。
>>>filterwarnings("error")
>>>warn("This function is really old...", DeprecationWarning)
Traceback (most recent call last):
File "<input>", line 1, in <module>
DeprecationWarning: This function is really old...
#根据异常来过滤特定类型的警告
>>>filterwarnings("ignore", category=DeprecationWarning)
>>>warn("Another deprecation warning.", DeprecationWarning)
>>>warn("Something else.")
Traceback (most recent call last):
File "<input>", line 1, in <module>
UserWarning: Something else.
边栏推荐
- 事务的传播机制
- 第十六章:构建n(5,7)阶素数幻方
- Install the gstreamer development dependency library to the project sysroot directory
- Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
- 电压源的电路分析知识分享
- 什么是半波整流器?半波整流器的使用方法
- 双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
- 线程唤醒机制
- MySql的安装配置超详细教程与简单的建库建表方法
- 2022.07.20_每日一题
猜你喜欢

In-depth analysis of z-index

DirectExchange switch simple introduction demo

【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)

03-SDRAM: Write operation (burst)

Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software

【Go报错】go go.mod file not found in current directory or any parent directory 错误解决

熟悉而陌生的新朋友——IAsyncDisposable

毫米波技术基础

测试 思维导图

【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
随机推荐
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
leetcode 406. Queue Reconstruction by Height
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
测试 思维导图
04-SDRAM: Read Operation (Burst)
Database Principles Homework 3 — JMU
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
【Go】Go 语言切片(Slice)
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
2022.07.22_每日一题
How to choose a suitable UI component library in uni-app
深度学习通信领域相关经典论文、数据集整理分享
shell之条件语句(test、if、case)
DirectExchange switch simple introduction demo
Gradle剔除依赖演示