How should I use try...except while defining a function?
        Posted  
        
            by SpawnCxy
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by SpawnCxy
        
        
        
        Published on 2010-04-01T12:04:26Z
        Indexed on 
            2010/04/01
            12:13 UTC
        
        
        Read the original article
        Hit count: 203
        
Hi all,
I find I've been  confused by the problem that when I needn't to use try..except.For last few days it was used in almost every function I defined  which I think maybe a bad practice.For example:
class mongodb(object):
    def getRecords(self,tname,conditions=''):
        try:
            col = eval("self.db.%s" %tname)
            recs = col.find(condition)
            return recs
        except Exception,e:
            #here make some error log with e.message
What I thought is ,exceptions may be raised everywhere and I have to use try to get them. 
And my question is,is it  a good practice to use it everywhere when defining functions?If not  are there any principles for it?Help would be appreciated!
Regards
© Stack Overflow or respective owner