If an error occurs within the cmd method, an exception object of the DBMServError class is generated. This exception can be identified and handled within a try-except statement.
# Reference to Python Libraries
# ----------------------------
import sys
import sdb.dbm
# To create a Database Manager session:
# ------------------------------------------------------
dbm_user_name, dbm_user_password, database_name = sys.argv [1:4]
session = sdb.dbm.DBM ('', database_name, '', dbm_user_name + ',' + dbm_user_password)
for cmd in ['db_state', 'invalid command']:
try:
result = session.cmd (cmd)
# To output the result:
print cmd + ': OK ', repr (result)
except sdb.dbm.DBMServError, err:
# To output the error message:
print cmd + ': ERR', err.errorCode, err.message