语法:
# 如果没出错,else语句就执行
try:
raise OSError('手动指明的原因')
except OSError as reason:
print('异常', str(reason))
else:
# 无法执行到的代码
print('程序无异常')
finally:
print('反正要执行到这里')
示例:
connection = engine.connect(close_with_result=True)
trans = connection.begin()
try:
print('start1111')
# source user 扣除金额
step1 = session.query(Users_table).filter(Users_table.username == source_user).first()
s_money = session.query(Finance_table).filter(Finance_table.user_id == step1.id).first()
if s_money.money < 0:
# raise UserWarning(f'{source_user}余额不足,转账失败')
print(f'{source_user}余额不足,转账失败')
trans.rollback()
session.query(Finance_table).filter(Finance_table.user_id == step1.id).update(
{Finance_table.money: s_money.money - trans_money})
# destination_user 增加金额
step2 = session.query(Users_table).filter(Users_table.username == destination_user).first()
d_money = session.query(Finance_table).filter(Finance_table.user_id == step2.id).first()
session.query(Finance_table).filter(Finance_table.user_id == step2.id).update(
{Finance_table.money: d_money.money + trans_money})
# 添加日志
trans_log = Finance_transfer_detail_table(user_from=step1.id,user_to=step2.id,money=trans_money)
session.add(trans_log)
session.commit()
except Exception as e:
print(e)
trans.rollback()
raise
finally:
print('trans module ok end')
connection.close()
print('this is the end')
raise 会手工引发代码异常,从而中断程序运行,用于程序失控时强行终止程序。
异常标准类:
python所有的标准异常类:
| 异常名称 | 描述 |
|---|---|
| BaseException | 所有异常的基类 |
| SystemExit | 解释器请求退出 |
| KeyboardInterrupt | 用户中断执行(通常是输入^C) |
| Exception | 常规错误的基类 |
| StopIteration | 迭代器没有更多的值 |
| GeneratorExit | 生成器(generator)发生异常来通知退出 |
| SystemExit | Python 解释器请求退出 |
| StandardError | 所有的内建标准异常的基类 |
| ArithmeticError | 所有数值计算错误的基类 |
| FloatingPointError | 浮点计算错误 |
| OverflowError | 数值运算超出最大限制 |
| ZeroDivisionError | 除(或取模)零 (所有数据类型) |
| AssertionError | 断言语句失败 |
| AttributeError | 对象没有这个属性 |
| EOFError | 没有内建输入,到达EOF 标记 |
| EnvironmentError | 操作系统错误的基类 |
| IOError | 输入/输出操作失败 |
| OSError | 操作系统错误 |
| WindowsError | 系统调用失败 |
| ImportError | 导入模块/对象失败 |
| KeyboardInterrupt | 用户中断执行(通常是输入^C) |
| LookupError | 无效数据查询的基类 |
| IndexError | 序列中没有没有此索引(index) |
| KeyError | 映射中没有这个键 |
| MemoryError | 内存溢出错误(对于Python 解释器不是致命的) |
| NameError | 未声明/初始化对象 (没有属性) |
| UnboundLocalError | 访问未初始化的本地变量 |
| ReferenceError | 弱引用(Weak reference)试图访问已经垃圾回收了的对象 |
| RuntimeError | 一般的运行时错误 |
| NotImplementedError | 尚未实现的方法 |
| SyntaxError | Python 语法错误 |
| IndentationError | 缩进错误 |
| TabError | Tab 和空格混用 |
| SystemError | 一般的解释器系统错误 |
| TypeError | 对类型无效的操作 |
| ValueError | 传入无效的参数 |
| UnicodeError | Unicode 相关的错误 |
| UnicodeDecodeError | Unicode 解码时的错误 |
| UnicodeEncodeError | Unicode 编码时错误 |
| UnicodeTranslateError | Unicode 转换时错误 |
| Warning | 警告的基类 |
| DeprecationWarning | 关于被弃用的特征的警告 |
| FutureWarning | 关于构造将来语义会有改变的警告 |
| OverflowWarning | 旧的关于自动提升为长整型(long)的警告 |
| PendingDeprecationWarning | 关于特性将会被废弃的警告 |
| RuntimeWarning | 可疑的运行时行为(runtime behavior)的警告 |
| SyntaxWarning | 可疑的语法的警告 |
| UserWarning | 用户代码生成的警告 |
祝看到此篇文章的人早日封神