Data girl
Data girl

Reputation: 177

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

Upvotes: 4

Views: 6417

Answers (1)

paul41
paul41

Reputation: 676

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

Upvotes: 3

Related Questions