[Tutor] general exception questions

Laura Creighton lac at openend.se
Tue Oct 6 00:13:06 CEST 2015


In a message of Mon, 05 Oct 2015 22:39:13 +0100, Mark Lawrence writes:
>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

I think this depends on what the script is doing.  For commerical,
production use you generally want to find out what the errors are
and do something reasonable for them -- but for my own use there are
plenty of times when 

I want to run this thing once.
If it runs into trouble I want it to keep on trying to do its thing.
The only thing I might consider doing with any exceptions that get
raised is logging them before ignoring them.
The worst thing that could happen is for the script to quit running
after a few hours because some wretched exception I wasn't ready for
happened.  This means I will have to add that exception to the list of
things to ignore and run the thing again next night ...

Laura




More information about the Tutor mailing list