Let me sum up :
I have a module containing two classes (note : this module is in a package) :
a custom Exception :
class MYAUTHError(Exception):
def __init__(self, *args, **kwargs):
print('--- MYAUTHError!!! ---')
and a class using this exception (here a sample) :
try:
resp_login.raise_for_status()
except requests.exceptions.HTTPError as ex:
logging.error("ERROR!!! : user authentication failed)
raise MYAUTHError('oups')
Inside this module (file) i know this works. For exemple, I can code things like this and verify that my custom exception is caught :
try:
raise MYAUTHError('oups')
except MYAUTHError:
print("got it")
However, when used from another module (a module importing this module), I do not succeed catching this custom exception...
from mypackage import mymodulewithexception
# somewhere in the code, just to test. OK : my class is known.
extest = mymodulewithexception.MYAUTHError('**-test-**')
print(type(extest))
# but this does not catch anything :
except mymodulewithexception.MYAUTHError as ex:
logging.error("Authentication failed", ex)
return
I'm sure, the exception is thrown, because the calling module is a flask app and the debug server clearly show me the exception is thrown because it is not handled.
While trying to understand this, I simply replaced my custom exception with another famous exception : ValueError. I change code in the calling module to catch this : This worked, of course.
I even tried, to just catch a Exception (the really generic class) :
except mymodulewithexception.MYAUTHError as ex:
print("got it")
except Exception as ex:
print('----------------------------------------------------')
print(ex)
print('-------------------')
My custom exception is ignored in the first catch, but caught on the second one...
How can it be my custom exception is not caught properly? Perhaps, the package context?
Thanks for your help!