[Tutor] general exception questions

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Oct 5 23:39:13 CEST 2015


On 05/10/2015 18:46, richard kappler wrote:
> I'm reading up on exception handling, and am a little confused. If you have
> an exception that just has 'pass' in it, for example in a 'for line in
> file:' iteration, what happens? Does the program just go to the next line?
>
> EX:
>
> for line in file:
>      try:
>          do something
>      except:
>          pass
>
> I know (so many of you have told me :-) that using pass is a bad idea, but
> how else do you skip the current line if the 'try' can't be done, and go on
> to the next line exiting the program with a trace error?
>
> regards, Richard

Don't put the `try: except:` there in the first place.  This is the best 
way to approach development.  Let Python throw all your programming 
errors at you, then refine your code to catch the ones you need to.  See 
https://docs.python.org/3/library/exceptions.html#exception-hierarchy 
for the ones you would potentially need to catch for file handling under 
OSError.

One must also add the obligatory never use a bare `except:`

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list