[Tutor] Script performance: Filtering email

D-Man dsh8290@rit.edu
Tue, 16 Jan 2001 12:41:51 -0500


On Tue, Jan 16, 2001 at 02:29:17AM -0800, Sheila King wrote:
| On Tue, 16 Jan 2001 10:51:54 +0100, Remco Gerlich <scarblac@pino.selwerd.nl>
[snip]
| :Maybe you should wrap the whole thing in a try/except to log whatever
| :goes wrong, like
| :
| :try:
| :   (the body of your script)
| :except:
| :   import sys
| :   f=open("somelogfile","w")
                            ^

Don't use "w" (write mode) here.  It will clobber the existing file.
Use "a" (append mode) instead.  I would also suggest printing some
text so that you know where the different errors are.  (see 3 lines
farther for an examle)

| :   print >> f, sys.exc_info()
      print >> f, "\n\n*****My Error Separator line****\n\n"
| :   f.close()
| :   
| :   raise # Re-raise the exception so that the program doesn't quit silently
| :   
| :If that log file fills up, you know where to look. I can't really
| :say more from here.
| 
| I've never worked with exceptions before. Is it really that easy? I will try

They are quite easy when you know what you want to do with them.  In
this case you just want to report it to a log file so it is easy.  In
some cases, such as a complete gui based application, deciding what do
to isn't so easy (this is no different than dealing with "traditional"
error handling such as in C).  But the exception handling itself isn't
hard (and I think is much better and easier than the nested if's that
are very common in C).

| it out tomorrow and see what I get in the log file. Surely something.
| 
| Is the additional "import sys" command inside the "except" block necessary if
| I've already imported that module at the beginning of my script?

No.  Remco probably put it there just to be on the safe side (or maybe
he forgot you already imported it).  The actual import will only
happen once.  

<Tangent>
If you want to force the interpreter to import the module
again and run the module's init code use the reload function :

reload( sys )

</Tangent>


-D