what exceptions may file() and read() throw?

D

Daniel Schüle

Hello,

currently I am using this instance method

def getFilecontent(self, filename):
try:
return file(filename).read()
except IOError, err_msg:
print err_msg
sys.exit(1)
except:
print "unknown exception in PackageParser"
sys.exit(1)

I tried to open a file for which I don't have the permissions to read
(etc/shadow)
and I tried to open a file which doesn't exist
in both cases I got IOError exception, so my question is
does it make sence to have

except:
print "unknown exception in PackageParser"
sys.exit(1)

or is it a dead code?
are there some good reference sources to see what file() and read()
may throw, IMHO it's OS dependent.

Regards, Daniel
 
D

Duncan Booth

Daniel said:
Hello,

currently I am using this instance method

def getFilecontent(self, filename):
try:
return file(filename).read()
except IOError, err_msg:
print err_msg
sys.exit(1)
except:
print "unknown exception in PackageParser"
sys.exit(1)

I tried to open a file for which I don't have the permissions to read
(etc/shadow)
and I tried to open a file which doesn't exist
in both cases I got IOError exception, so my question is
does it make sence to have

except:
print "unknown exception in PackageParser"
sys.exit(1)

or is it a dead code?

It is dead code in the sense that it simply converts every exception into a
SystemExit exception thereby losing information from the message and the
traceback which might have helped you track down the problem. If you aren't
able to do anything useful with an exception then the best thing is almost
always to let it propogate upwards to somewhere it can be handled usefully.
That generally means that exceptions you haven't thought of need to be
communicated to a human who can lodge a bug report or fix the code.

You can't easily list the exceptions that your code could throw. There are
some obvious ones apart from IOError: say filename was an int (or even
certain strings) you would get TypeError, or you might get MemoryError or
KeyboardInterrupt. More obscurely, if you reused file as a global variable
you could generate any exception at all.
 
D

Daniel Schüle

Hi
You can't easily list the exceptions that your code could throw. There are
some obvious ones apart from IOError: say filename was an int (or even
certain strings) you would get TypeError, or you might get MemoryError or
KeyboardInterrupt. More obscurely, if you reused file as a global variable
you could generate any exception at all.

I undestand now, so it would be better to let it in the code
in case it's triggered (I triggered it by passing int instead of str, as you
said)
I would at least know where to look
I wouldn't call it "dead code" then ... dead code is more like

if False:
do_something()

thank you

Regards, Daniel
 
B

Bruno Desthuilliers

Daniel said:
Hi




I undestand now, so it would be better to let it in the code
in case it's triggered

Nope. Let it propagate, so you have a full traceback. traceback are usefull.
 
W

wittempj

Daniel said:
Hello,

currently I am using this instance method

def getFilecontent(self, filename):
try:
return file(filename).read()
except IOError, err_msg:
print err_msg
sys.exit(1)
except:
print "unknown exception in PackageParser"
sys.exit(1)

I tried to open a file for which I don't have the permissions to read
(etc/shadow)
and I tried to open a file which doesn't exist
in both cases I got IOError exception, so my question is
does it make sence to have

except:
print "unknown exception in PackageParser"
sys.exit(1)

or is it a dead code?
are there some good reference sources to see what file() and read()
may throw, IMHO it's OS dependent.

Regards, Daniel

You also could do something like

def getFilecontent(self, filename):
try:
return file(filename).read()
except IOError, err_msg:
print err_msg
sys.exit(1)
except Exception ,e:
print e.__class__, str(e)

Then the next tiome this happens you have more information on what
happened
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top