How to handle exceptions properly in a pythonic way?

Chris Angelico rosuav at gmail.com
Wed Nov 4 22:58:33 EST 2015


On Thu, Nov 5, 2015 at 2:41 PM,  <zljubisic at gmail.com> wrote:
> Raising an exception forces me to put every call of the get_html function in try/except block.
> If I am returning a special value, than I can call get_html and then test the value.
>
> I am not sure which approach is better.

Raising an exception means that if you fail to put it in a try/except,
you get a traceback that walks you through the exact location of the
problem.

Returning a special value means that if you fail to test the value,
you either get an exception further on (if you get back None and try
to treat it as a string, for instance), or get incorrect results, with
no indication of the actual cause of the problem.

Which would you prefer?

ChrisA



More information about the Python-list mailing list