Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failed: exceptions must derive from BaseException

Tags:

python

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

like image 488
Data girl Avatar asked Oct 16 '25 06:10

Data girl


1 Answers

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
like image 58
paul41 Avatar answered Oct 17 '25 19:10

paul41



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!