strange subprocess behavior when calling ps

Chris Rebert crebert at ucsd.edu
Wed Nov 17 20:43:14 CET 2010


On Wed, Nov 17, 2010 at 11:29 AM, Roger Davis <rbd at hawaii.edu> wrote:
>> Completely off topic but I think the try clause could be rewritten that way:
>> ...
>> Don't use bare except clause, you're masking syntax errors for instance,
>> which will be flagged as 'unexpected error in generation ...".
>> In a more general manner, if something unexpected happens it's better to
>> just let the exception raise uncought. If you want to handle some
>> errors, meaning you're kindof expecting them then add a explicit clause
>> (like you did with KeyboardInterrupt).
>>
>> JM
>>
>> PS : "except Exception :" will catch most of the exceptions (all
>> inheriting from that class). It's better than using a bare "except :"
>> clause. (Exception won't catch SyntaxError)
>
> Thanks for the suggestion JM, it is off-topic and, although I will
> first just say that the exception mechanism is *not* one of the
> reasons I use Python (and stop there with regard to the whole
> exception mechanism and various usage strategies in general), I do
> have a few specific questions about a couple of your statements if you
> don't mind following up.
>
> First, inserting a syntax error into my existing code does not hide a
> SyntaxError exception as you have stated:
<snip>
> % ./pid.py
>  File "./pid.py", line 14
>    if pslines not good Python talky-talk
>                      ^
> SyntaxError: invalid syntax
>
> It appears that the interpreter is catching the syntax error before
> the code is even executed.

Now try:

# junk.py
@#$%^& gibberish @#$%^

# main.py
import junk

You'll get a run-time SyntaxError.

<snip>
> Finally, and this does not apply to your comments in particular, in
> researching around about exception handling I often see the usage
>
>   except Exception, e:
>
> suggested, but can't for the life of me figure out what the heck the
> ', e' part does. Can anybody explain what this means and why it might
> be desirable (or not)?

That's how `except Exception as e` used to be spelled. The exception
object will be bound to the variable `e`, allowing you to inspect it
further.
For example, one might use that form of `except` with an I/O-related
exception to be able to check its `errno` attribute in order to handle
the error properly.
If you're just catching a generic Exception, said form of `except` is
less useful.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list