Questions on exceptions

sc_wizard29 at hotmail.com sc_wizard29 at hotmail.com
Mon Aug 21 03:40:14 EDT 2006


Hi everyone,

I've just finished studying O'Reilly's "Learning python" and since I
come from the Java world, there are some things that bother me
concerning python's exception handling.

In Java, all methods must declare the exceptions throwed (I'm speaking
of checked exceptions)... but this is not the case in Python. So my
question is : how can I know which exceptions I must catch ? (am I
supposed to believe what's written in the documentation ? am I supposed
to read the source code to see which exceptions are throwed ?)

Also, can someone explain me why there is no try...except...finally
statement ? For example, the following code snippet is not valid, but
what would be the correct python way to do it ?

myFile = open('file.txt') # assume file exists
try:
	for nextLine in file:
        	nextLine = nextLine.rstrip('\n');print "line = " + nextLine
except IOError:
	print "Error while reading from file"
finally:
	myFile.close




More information about the Python-list mailing list