[openstack-dev] [Review] Use of exception for non-exceptional cases

David Ripton dripton at redhat.com
Wed Jul 10 19:57:19 UTC 2013


On 07/10/2013 02:01 PM, Nachi Ueno wrote:
> HI folks
>
> I would like to ask the review criteria in the community.
>
> Should we use exception for non-exceptional cases when we can use
>   parameter checking?
>
> Example1:  Default value for array index
>
> try:
>     value = list[5]
> except IndexError:
>      value = 'default_value'
>
> This can be also written as,
>
>       list_a[3] if len(list_a) > 3 else 'default_value'

Both of these are fine.  Neither deserves to be banned.

But LBYL is often naive in the face of concurrency.  Just because 
something was true a microsecond ago doesn't mean it's still true. 
Exceptions are often more robust.

-- 
David Ripton   Red Hat   dripton at redhat.com



More information about the OpenStack-dev mailing list