When a module class is not in my script but used by one of the modules (I did imported explicitly) how do I catch his error?
For example:
from sqlite3 import dbapi2 as sqlite
class sqldb:
def __init__(self):
self.sqlite.connect('records.db')
self.c = self.conn.cursor()
def query(self,query,values)
try:
self.c.execute(query, values)
self.conn.commit()
except sqlite3.OperationalError:
print "SQLite DB locked"
Will result in (when the database is locked):
NameError: global name 'sqlite3' is not defined
But when I don’t catch the error it gives me exactly that exception: ‘sqlite3.OperationalError’
So what should I put as Except ? Or should I just import the whole sqlite3 module? If yes, doesn’t this increase the resources footprint of my program?
Yes.
No — the module is imported anyway. All your
importstatement does is to add a reference tosqlite3to your module’s global namespace.