我正在尝试捕获函数调用中抛出的特定类型的异常.我在try / except块中包含了函数调用,其中except块捕获了抛出的特定异常.我仍然得到该异常的系统失败堆栈跟踪,除非我还包含所有异常的常规catch.在包含该块并检查被捕获的异常的类型时,我看到它正在捕获我想要在第一个块中捕获的异常类型.不知道为什么会这样.
上下文:使用webapp2和ndb处理谷歌应用引擎应用.文件函数有一个init.py,它从exceptions.py导入所有异常
模拟代码和结构
utils的/功能/ exceptions.py
"""
Custom exception types
"""
class InvalidParamsException(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return repr(self.msg)
车型/ models.py
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import utils.functions as func
<-->
class ModelClass(ndb.Model):
@classmethod
def new(cls):
<-->
raise func.InvalidParamsException("Invalid Params to function!")
<-->
routes.py
import utils.functions as func
from models import ModelClass
class ModelClassHandler(webapp2.RequestHandler):
def post(self):
try:
new_model = ModelClass.new()
except func.InvalidParamsException as e:
logging.debug("Caught the right Exception!!")
except Exception as e:
logging.debug(":(")
logging.debug("EXCEPTION TYPE - %s"%str(type(e)))
如果我不包括第二个常规除了块,我得到的输出是:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "{{my_path}}/routes.py", line 58, in post
new_model = ModelClass.new()
File "{{my_path}}/models/models.py", line 559, in new
raise func.InvalidParamsException("Invalid Params to function!")
InvalidParamsException: 'Invalid Params to function!'
如果我确实包含第二个块,我会优雅地传递路由/函数,并在日志中看到:
DEBUG 2016-03-25 01:01:03,221 routes.py:66] EXCEPTION TYPE - <class 'utils.functions.exceptions.InvalidParamsException'>
帮助/指导非常感谢!!
最佳答案 看起来Python引发了在当前命名空间中导入的异常.我最好的evicende是事实,回溯的最后一行调用引发的异常“InvalidParamsException”而不是“somemodule.InvalidParamsException”.
因此,我建议解决名称空间冲突,将异常明确地导入到“routes.py”的命名空间中:
来自utils.functions.exceptions导入InvalidParamsException
并通过其现在解析的命名空间名称捕获异常:
除了InvalidParamsException为inv_param_exp:
< …>