I have a simple plugin system, simplified form below. Idea is that plugins will implement an abstract class and can raise an exception to signal teardown.
# my_plugin.py
import my_app
class MyPlugin(my_app.MyPluginBase):
def start(self):
raise my_app.MyLibException()
# my_app.py
import abc
import importlib
class MyLibException(Exception):
pass
class MyPluginBase(abc.ABC):
@abc.abstractmethod
def start(self):
pass
def main():
module = importlib.import_module('my_plugin')
klass = getattr(module, 'MyPlugin')
try:
app = klass()
app.start()
except MyLibException as e:
print('ok')
print(e.__class__)
except Exception as e:
print('not ok')
print(e.__class__)
if __name__ == '__main__':
main()
Running above results in:
not ok
<class 'my_app.MyLibException'>
What's the correct way of handling exceptions for such a scenario? I would like to catch the raised exception here except MyLibException as e:
rather than except Exception as e:
.