我有很多模塊。他們都有類似的嘗試,除了在每個文件塊,像這樣:簡化Python中的try-except塊
from shared.Exceptions import ArgException # and others, as needed
try:
do_the_main_app_here()
except ArgException as e:
Response.result = {
'status': 'error',
'message': str(e)
}
Response.exitcode('USAGE')
# more blocks like the above
與ArgException(和其他異常)被定義爲:
from abc import ABCMeta, abstractmethod
class ETrait(Exception):
__metaclass__ = ABCMeta
@abstractmethod
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class ArgException(ETrait): pass
由於每個模塊都使用類似的代碼捕獲異常,有沒有辦法將異常捕捉到所有模塊使用的共享文件中?