繁体   English   中英

失败:异常必须从 BaseException 派生

[英]failed: exceptions must derive from BaseException

我试图在这里避免 BaseException 问题。 我的要求是我有一个自定义异常。 当引发自定义异常时,我需要执行一些命令并需要中止作业。

class custom_exp(Exception):
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return repr(self.msg)

try:
   #calculating a threshold value 
 # if( above threshold):
     raise custom_exp("hello")

except custom_exp as x:
    # have some code to store the bad records
    raise(message) # this line written to fail the job 
     
except Exception as e:
    # have some code to store the bad records
       raise(str(e))  # this line written to fail the job 

这是输出


  File "C:\Users\gopia\OneDrive - VW Credit\Desktop\untitled1.py", line 11, in <module>
    raise(message)

TypeError: exceptions must derive from BaseException

你可以做这样的事情

class CustomExp(Exception):
    pass


try:
    # calculating a threshold value
    # if above threshold:
    raise CustomExp("hello")
except CustomExp as e:
    # store the records
    raise e
except Exception as e:
    # store the records
    raise e

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM