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. -- http://mail.python.org/mailman/listinfo/python-list