I am trying to define a custom Exception class so that I can handle a specific type of error in my algorithm. 

I'm using a bog-standard way of defining custom exceptions: 

Inside MyCustomException.py: 

class MyCustomError(Exception): """Custom exception""" def __init__(self, msg): super().__init__(msg)

Inside main.py:

import MyCustomException try: # two instruments for pairs trading sym1, sym2 = pair_tuple # handle the case when we can't find the symbol in question inside dictionary self.last_prices try: last_close1 = self.last_prices[sym1] last_close2 = self.last_prices[sym2] except KeyError: # catch the KeyError and raise a specific error for handling. raise MyCustomException.MyCustomError(f"{pair_tuple} not found") # some other code.... except MyCustomException.MyCustomError as e: # some error handling for this specific error

However, when I try this I keep getting the following error:

TypeError : catching classes that do not inherit from BaseException is not allowed

It looks like there's nothing wrong with my code - the exception class is inheriting from Exception which is confirmed when looking at MyCustomException.MyCustomError.__bases__. The one thing I think may be causing this is when I take a look at type(MyCustomException.MyCustomError), this returns "clr.clr MetaClass". Maybe this MetaClass is interfering with the logic in the except clause?