Error handling in context managers

Israel Brewster israel at ravnalaska.net
Tue Jan 17 13:55:35 EST 2017


On Jan 16, 2017, at 8:01 PM, Gregory Ewing <greg.ewing at canterbury.ac.nz> wrote:
> 
> Israel Brewster wrote:
>> The problem is that, from time to time, I can't get a connection, the result
>> being that cursor is None,
> 
> That's your problem right there -- you want a better-behaved
> version of psql_cursor().
> 
> def get_psql_cursor():
>   c = psql_cursor()
>   if c is None:
>      raise CantGetAConnectionError()
>   return c
> 
> with get_psql_cursor() as c:
>   ...
> 

Ok, fair enough. So I get a better exception, raised at the proper time. This is, in fact, better - but doesn't actually change how I would *handle* the exception :-)
-----------------------------------------------
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
-----------------------------------------------

> -- 
> Greg
> -- 
> https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list