Question about exception-handling mechanism

Moshe Zadka moshez at math.huji.ac.il
Tue Apr 25 19:47:55 CEST 2000


On Tue, 25 Apr 2000 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

Lots of time.

Consider some sort of

def f(file):
	try:
		fp = open(file)
	except IOError:
		return 0 # it doesn't exist
	else:
		return fp.read(2)=='PK'

To check if a certain file is both readable and is a zip-file. You don't
want the except to cover the "read" -- an error in read is unexpected.
--
Moshe Zadka <mzadka at geocities.com>. 
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com





More information about the Python-list mailing list