1👍
The problem was that a different exception class was being thrown up by models.py, though the name was the same.
My settings.py did not specify the full path of the app where the models.py in question lived. After specifying the full path, the exception classes matched and the exception was caught. Thanks to all who provided great hints.
0👍
It’d help to see more of your code around the exception catching. From what you have shown, there are a few things to look at:
- I’m assuming
TemplateMatchError
is what you originally calledMyError
- Your code seems unsure of how
template.match
returns a negative result. Inserializers.py
, it seems to expect anil
/false
return value but the function itself raises an exception rather than returning something falsey. - The code segment as you’ve shown it has bad indentation, which could be causing the failure to catch the error.
As you’ve shown it:
try:
template.match(text)
# Do other stuff, presumably including this:
try:
somethingElse()
except TemplateMatchError as e:
#this won't catch exceptions from template.match(text)
continue
How I think you mean it:
try:
template.match(text)
except TemplateMatchError as e:
# This will be caught
continue
Hope that helps.
- [Answer]-Django Admin Custom Button Not Working
- [Answer]-How to minify JS with Django templates, and RequireJS
- [Answer]-Merge fields in user created email templates in django
- [Answer]-Convert Django ModelForm to use User object
0👍
are you sure that it is the same Class «TemplateMatchError» imported from the same module that you raise and you try to catch.
if this is two class with the same name but imported from diferent module, python won’t trait them as the same Exception, an then never enter your catch block.
- [Answer]-Django Ajax search will not work
- [Answer]-Converting from function- to class-based view
- [Answer]-Django. Specific error handling, without including try-catch in every calling function
- [Answer]-How do I get my pinterest share button to render my custom image in Django?
- [Answer]-Tastypie random queryset results
0👍
Modify the code this way in order to verify assumptions at very near point.
import api
assert TemplateMatchError == api.models.TemplateMatchError
try:
if template.match(text):
return attrs
except TemplateMatchError as e:
continue
except Exception as e:
assert isinstance(e, TemplateMatchError)
import pdb; pdb.set_trace()
pass # if both asserts vere OK, but the exception is uncaught (not
# probable) you are here and see the the line debugger started
raise # continue by caugting in external frames
Start the testserver by the best way for debugging
python manage.py runserver --nothreading --noreload
When you see the debugger prompt (Pdb)
, put these commands in order to repeat it step by step:
l(ist) Enter
j(ump) <line number of the line 'try:'> Enter
b /home/uname/api/models.py:135 Enter # breakpoint at that raise command
c(ontinue) Enter
s(tep) Enter # press Enter five times to see steps, how the lines
# "except ... continue" are missed
c(ontinue) # to reraise and see an error page in the browser
However I think that one of asserts will fail, if DEBUG=True, and you will know more, without debugger.
- [Answer]-Allow embed code on django template?
- [Answer]-Tango with Django tutorial: "table rango_category has no column named views"