On Mon, 21 Aug 2006 00:40:14 -0700, sc_wizard29 wrote: > 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 ?)
How do you know what objects a method will return? Are you supposed to believe the documentation? Are you supposed to read the source code? Exceptions are no different. If a method claims to raise ValueError, but sometimes raises TypeError, I call that a bug, regardless of whether the line which causes it is "int(x) + str(y)" or "raise TypeError('My documentation is incomplete')". > Also, can someone explain me why there is no try...except...finally > statement ? Historical reasons. I believe that Python 2.5 will support it. > 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 Nested try...except blocks. -- Steven D'Aprano -- http://mail.python.org/mailman/listinfo/python-list