Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion try/except KeyError vs "if name in ..."
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ramchandra Apte  
View profile  
 More options Oct 7 2012, 12:17 am
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Sat, 6 Oct 2012 21:17:33 -0700 (PDT)
Local: Sun, Oct 7 2012 12:17 am
Subject: Re: try/except KeyError vs "if name in ..."

On Sunday, 7 October 2012 01:12:56 UTC+5:30, Terry Reedy  wrote:
> On 10/6/2012 7:36 AM, Dave Angel wrote:

> > The distinction in performance between the success and failure modes of

> > the try/catch isn't nearly as large as one of the other responses might

> > lead you to believe.  For example, a for loop generally terminates with

> > a raise (of StopIteration exception), and that doesn't convince us to

> > replace it with a while loop.

> For statement generally loop many times, up to millions of times,

> without an exception being raised, whereas while statements test the

> condition each time around the loop. So the rule 'if failure is rare

> (less than 10-20%) use try', applies here. For if/them versus

> try/except, I don't worry too much about it.

> --

> Terry Jan Reedy

I use try and except when I need to raise exceptions e.g.:

try:
    value = vm.variables[name]
except KeyError:
    raise NameError("variable name not defined in VM's variables")


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.