首页 > 编程语言 >Python: Exception

Python: Exception

时间:2023-03-28 16:24:54浏览次数:40  
标签:__ Exception .__ Python traceback exception try

 

raise from

try:
    try:
        1 / 0
    except ZeroDivisionError as e:
        raise Exception("with_traceback") from e
except Exception as e:
    print(e, e.__cause__, type(e.__cause__))

try:
    1 / 0
except ZeroDivisionError as e:
    raise Exception("with_traceback") from e

 

 

The bottom exception only has the stacktrace from where we raised our exception. Notice the bottom exception only has the stacktrace where we raised our exception. Your caller could still get the original exception by accessing the __cause__ attribute of the exception they catch.

 

with_traceback

try:
    try:
        1 / 0
    except ZeroDivisionError as e:
        raise Exception("with_traceback").with_traceback(e.__traceback__)
except Exception as e:
    print(e, e.__cause__, type(e.__cause__))

try:
    1 / 0
except ZeroDivisionError as e:
    raise Exception("with_traceback").with_traceback(e.__traceback__)

 

 

Using this form, the exception your caller would catch has the trackback from where the original error occurred. Notice the bottom exception has the line where we performed the invalid division as well as the line where we reraise the exception.

 

Custom Exception

class CloudFirewallError(Exception):
    def __init__(self, message: str):
        super().__init__(f'CloudFirewallError: {message}')


class ApiError(Exception):
    def __init__(self, status_code):
        if status_code == 403:
            message = 'Rate limit reached. Please wait a minute and try again'
        else:
            message = f'HTTP status code: {status_code}'
        super().__init__(message)

 

标签:__,Exception,.__,Python,traceback,exception,try
From: https://www.cnblogs.com/dissipate/p/17265645.html

相关文章

  • opencv-python 4.2图像的几何变化
    转换OpenCV提供了两个转换函数cv.warpAffine和cv.warpPerspective,你可以使用它们进行各种转换。cv.warpAffine采用2x3变换矩阵作为参数输入,而cv.warpPerspective采用3x3变......
  • python generator相关
    本文的重点介绍python中的yield用法及这样的表达式:comp_list=[x*2forxinrange(10)] -- ListComprehensions和(x**2forxinrange(10)) -- Generato......
  • python笔记3(字典)
    字典1.字典的创建1.{}2.dict()3.通过fromkeys创建值为空的字典2.字典元素的访问通过get()方法以a={"name":"jiachao","age":20,"school":"tongda"}为例如若结......
  • python笔记4(控制语句)
    控制语句一:选择结构A:单分支结构if(条件语句):(缩进)结果缩进:pytharm中默认四个空格=TAB键例:a=input("请输入一个小于10的数:")ifint(a)<10:print(a)运行结果:条......
  • 一直报错Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.j
    按照网上的提示在pom.xml添加了依赖<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></depen......
  • python 视频转代码视频
     #-*-coding:utf-8-*-#coding:utf-8importargparseimportosimportcv2importsubprocessfromcv2importVideoWriter,VideoWriter_fourcc,imread,resizefr......
  • Python & Anaconda 基础安装及配置
    原文链接:http://t.csdn.cn/nzH5w说明使用系统:Windows11本文暂时只包含最基础的PyCharm&Anaconda安装及环境配置,供自己记录使用,更为具体的配置方法请参考原文一、......
  • python3绕过360添加用户
    1.环境说明当前具有高权限账户会话,高权限webshell之类的当前环境下有360杀毒软件重点:需要有python3环境,如果没有,通过条件1上传python3环境2.python编写windows-api......
  • #Python 利用python计算百度导航骑行距离(第二篇)批量计算
    https://www.cnblogs.com/simone331/p/17218019.html在上一篇中,我们计算了两点的距离(链接为上篇文章),但是具体业务中,往往会存在一次性计算多组,上百甚至上千的距离。所以......
  • jmeter中通过PerfMon Metrics Collector插件监控CPU和内存等等显示ConnectException连
    一、问题定位1、jmeter中通过PerfMon Metrics Collector插件监控CPU和内存等等显示ConnectException连接超时2、定位到是因为服务器端没有启动serverAgent.sh代理,启动......