Question about exception-handling mechanism

Jerome Chan eviltofu at rocketmail.com
Tue Apr 25 10:05:25 EDT 2000


In article <8e46mt$4nh$1 at nnrp1.deja.com>, willfg at my-deja.com wrote:

> Hello all,
> 
>    I'm new to Python, but find its exception handling mechanism much
> more powerful than comparable languages; but a question was put to me
> that being new I can't come up with a very articulate answer. A
> colleague asked why in an exception handling mechanism you'd want the
> ELSE block to be executed if you don't throw an exception as opposed to
> a FINALLY block. Anyone used this feature in practice? Thanks in
> advance for your input, -- Will
> 
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.

According to the Python Pocket Reference

else: Run if no exceptions raised
finally: Always run on the way out

but the question I have is the following definations for the exception 
handling routines.

try: suite
[except [name [,data]]: suite]*
[else: suite]

try: suite
finally: suite

Does this mean I can't do
try:
    x = MyClass("s")
    # some other stuff that might raise an exception
except Exception,message:
    print message
else:
    print "something went wrong but I don't know what it is!"
finally:
    del x # or some other function

Also, is there a command line switch that would do what Java does when 
checking for exception handling? That the source code would be checked 
to see if any exceptions thrown are not caught? Or maybe a 3rd party 
utility?



More information about the Python-list mailing list