Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a exception with just a raise have any use?

For example, here is some code from django.templates.loader.app_directories.py.[1]

try:
    yield safe_join(template_dir, template_name)
except UnicodeDecodeError:
    # The template dir name was a bytestring that wasn't valid UTF-8.
    raise

If you catch an exception just to re raise it, what purpose does it serve?

[1] http://code.djangoproject.com/browser/django/trunk/django/template/loaders/app_directories.py

like image 864
agiliq Avatar asked Nov 28 '22 20:11

agiliq


1 Answers

In the code you linked to is another additional exception handler:

try:
    yield safe_join(template_dir, template_name)
except UnicodeDecodeError:
    # The template dir name was a bytestring that wasn't valid UTF-8.
    raise
except ValueError:
    # The joined path was located outside of template_dir.
    pass

Since UnicodeDecodeError is a subclass of ValueError, the second exception handler would cause any UnicodeDecodeError to be ignored. It looks like this would not be the intended effect and to avoid it the UnicodeDecodeError is processed explicitly by the first handler. So with both handlers together a ValueError is only ignored if it's not a UnicodeDecodeError.

like image 61
sth Avatar answered Dec 04 '22 22:12

sth