I have a module, and in this module I initialize some variables as soon as the module is imported.
my_mobule.py:
def _build_service():
# ...do some stuffs
_service = _build_service()
In this case, what will happen if the _build_service
method raises an exception? And how my module can recover from an exception and try to invoke the _build_service
again?
Thank you guys.
It's pretty similar to the behaviour should you call a function which raises an exception - if you don't handle the exception in the module itself, then it will be simply bump up the stack to whoever imported your module.
That would look like this:
>>> import my_mobule # sic
UhohError: something went wrong
If you have an opportunity to handle it in the module, you could do it like so:
try:
_service = _build_service()
except UhohError:
# your handling code here