473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Jun 23 '06 #1
4 1460
Daniel Schüle wrote:
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.

Jun 23 '06 #2
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
Jun 23 '06 #3
Daniel Schüle wrote:
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


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


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 23 '06 #4

Daniel Schüle wrote:
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

Jun 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no...
6
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.