我使用的是Django 1.4.5和Python 2.7.6。
我在exceptions.py中定义了自定义异常错误:
class RegEmailError(Exception):
pass
我也在另一个模块中提出它:
if not "success" in response:
from registration.exceptions import RegEmailError
raise RegEmailError(response)
我试图在另一个调用第二个模块中的函数的模块中捕获它:
from registration.exceptions import RegEmailError
try:
self._send_confirmation()
# RegEmailError is not getting caught here..
except RegEmailError as e:
print "send_confirmation: e", e
mail_admins("error", "Error sending out email(s): %s" % e)
if not fail_silently:
raise e
异常没有被捕获,而是在浏览器中显示回溯:
File "/Users/akulakov/envs/regproj/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Users/akulakov/envs/regproj/lib/python2.7/site-packages/django/views/generic/base.py", line 48, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/akulakov/envs/regproj/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in dispatch
return handler(request, *args, **kwargs)
File "/Users/akulakov/projects/regproj/htdocs/../htdocs/registration/views.py", line 122, in post
return super(RegisterView, self).post(request, *a, **kw)
File "/Users/akulakov/envs/regproj/lib/python2.7/site-packages/django/views/generic/edit.py", line 172, in post
return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/Users/akulakov/envs/regproj/lib/python2.7/site-packages/django/views/generic/edit.py", line 138, in post
return self.form_valid(form)
File "/Users/akulakov/projects/regproj/htdocs/../htdocs/registration/views.py", line 111, in form_valid
formset.save()
File "/Users/akulakov/projects/regproj/htdocs/registration/formset.py", line 107, in save
registrant.send_confirmation()
File "/Users/akulakov/projects/regproj/htdocs/registration/models.py", line 1011, in send_confirmation
self._send_confirmation()
File "/Users/akulakov/projects/regproj/htdocs/registration/models.py", line 1049, in _send_confirmation
util.send_email(tpl_key, '', to, subject, form_data)
File "/Users/akulakov/projects/regproj/htdocs/registration/util.py", line 122, in send_email
equeue.queue(template_key, to, from_, subject, form_data, **kw)
File "/Users/akulakov/projects/regproj/htdocs/registration/emailqueue.py", line 81, in queue
raise RegEmailError(response)
我以前没碰到过这个...我试过调试,看起来像是' e'是RegEmailError类型的正确例外,并且只有一个RegEmailError 在代码库中定义,在一个地方。