[Tutor] Which exceptions should I be catching?

W W srilyk at gmail.com
Mon Oct 20 23:17:28 CEST 2008


On Mon, Oct 20, 2008 at 3:30 PM, Wesley Brooks <wesbrooks at gmail.com> wrote:

> Unfortunately due to the nature of the program the error has normally
> happened hours ago and the error message has disappeared from the buffer of
> the command prompt.
>

That's why you should write an error log ;)


>
> This is the function:
>
> def CommandFileWriter(self, command):
>   name1 = os.path.join(self.commandsdir, command + '.temp')
>   name2 = os.path.join(self.commandsdir, command)
>   comfile = open(name1, 'w')
>   comfile.close()
>   if not os.path.exists(name2):
>     os.rename(name1, name2)
>   else:
>     os.remove(name1)
>
> This was the best way I could come up with doing the function. So the file
> is written to the correct directory with a wrong name (so the other program
> will ignore it) then it's name is changed to the correct name with
> os.rename. Unfortunately I think in freak occations the other program can
> read and delete the file (running on a multicore processor system) during
> the rename operation. Can you suggest which errors I should be trying to
> catch?
>

I'm not sure what errors to catch, but it's always possible to use a general
catchall (I'm not sure if this is particularly pythonic)

try:
    put your function here
except:
    print 'oops! an error occurred!'

Or do something besides print an error (such as write a message to a
logfile. If you include the time stamp and some other info that might be
helpful that may give you some more insight as to where the problem is, and
if it's something you can fix.)

HTH,
-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081020/703245ac/attachment.htm>


More information about the Tutor mailing list