Questions on exceptions

S

sc_wizard29

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
 
M

Marc 'BlackJack' Rintsch

sc_wizard29 said:
Also, can someone explain me why there is no try...except...finally
statement ?

AFAIK historical reasons. There where some concerns about ambiguity. But
in Python 2.5 this form will become legal syntax.
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

You forgot the parenthesis for the `close()` method.

In Python <=2.4 you have to nest:

try:
try:
pass
except Error:
pass
finally:
pass

Maybe you are interested in the new (Python 2.5) ``with`` statement too:

http://docs.python.org/dev/ref/with.html

And the style guide: http://www.python.org/dev/peps/pep-0008/ (because you
used Java naming conventions)

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top