简体   繁体   中英

failed: exceptions must derive from BaseException

I am trying to avoid the BaseException issue here . My requirement is i have a custom exception . when the custom exception is raised, i need to do some commands and need to abort a job.

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 

here is the output


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

TypeError: exceptions must derive from BaseException

You could do something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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