How to handle exceptions properly in a pythonic way?

zljubisic at gmail.com zljubisic at gmail.com
Mon Nov 9 16:43:42 EST 2015


Hi,

> def get_html(...):
>     try:
>         ... actually go get the info
>         return info
>     except (ConnectionError, OSError, SocketError) as e:
>         raise ContentNotFoundError from e

Personally, I never liked "early returns". I would rather used a variable and the last line in the function would be return variable.
Anyway, I got the point.

> To use this code, what you'd do is:
> 
> try:
>     data = get_html(...)
> except ContentNotFoundError:
>     # deal with the case of not having anything to work with

I got a picture. Thanks.

I have to rethink now about two approaches. One would be as you have suggested in this mail - raising ContentNotFoundError, and another one would be not to mask root exceptions and deal with them instead of ContentNotFoundError.

You have clarified to me the concept. Thanks.

Regards. 



More information about the Python-list mailing list