[Tutor] Which exceptions should I be catching?

W W srilyk at gmail.com
Tue Oct 21 00:40:29 CEST 2008


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

> That's why you should write an error log ;)
>
>
> The error log is a valid point. Is there a way to capture the error
> messages that go the the terminal window or command prompt rather than all
> my print statements? Can this be set up as a global thing for the whole
> application rather than inside each thread?
>
> try:
>>     put your function here
>> except:
>>     print 'oops! an error occurred!'
>>
>
> I'll probably resort to this and have done in a couple of other occasions,
> but thought it was generally frowned upon! I Guess I could do that and print
> out anything caught by the exception into a log and go more specific at a
> later date.
>
> Cheers,
>
> Wesley Brooks
>
>
>
>> 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.)
>>
>
This might help:
http://www.python.org/doc/2.5.2/lib/module-logging.html

I don't really know though, it was just the first thing that came up when I
entered "python logging"
HTH,
Wayne
-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levihttp://
www.python.org/doc/2.5.2/lib/module-logging.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081020/7b689086/attachment.htm>


More information about the Tutor mailing list