Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

python - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...