I am using PyMSSQL to connect to a database. However, if I enter the incorrect details or there is some other problem, PyMSSQL throws an exception. However, I cannot work out how to catch the exception. I've used every variation I can think of but I can't seem to catch the exception in a graceful way.
My connection code is:
import pymssql
import getpass
tempServer = input("Enter host: ")
tempUser = input("Enter user: ")
tempPwd = getpass.getpass("Enter password: ")
try:
phjTempConnection = pymssql.connect(server = tempServer,
user = tempUser,
password = tempPwd,
port = '1433')
except pymssql.MSSQLDatabaseException:
print("ERROR")
If I enter nonsense into the input fields, I get the following output:
---------------------------------------------------------------------------
MSSQLDatabaseException Traceback (most recent call last)
pymssql.pyx in pymssql.connect (pymssql.c:10734)()
_mssql.pyx in _mssql.connect (_mssql.c:21821)()
_mssql.pyx in _mssql.MSSQLConnection.__init__ (_mssql.c:6581)()
_mssql.pyx in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:17524)()
MSSQLDatabaseException: (18456, b'Unknown error')
During handling of the above exception, another exception occurred:
OperationalError Traceback (most recent call last)
<ipython-input-21-22d7fd0e3d05> in <module>()
11 password = tempPwd,
---> 12 port = '1433')
13 except pymssql.MSSQLDatabaseException:
pymssql.pyx in pymssql.connect (pymssql.c:10824)()
OperationalError: (18456, b'Unknown error')
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-21-22d7fd0e3d05> in <module>()
11 password = tempPwd,
12 port = '1433')
---> 13 except pymssql.MSSQLDatabaseException:
14 print("ERROR")
AttributeError: 'module' object has no attribute 'MSSQLDatabaseException'
I would have thought the output would provide enough information to be able to work out how to catch the MSSQLDatabaseException exception but I've tried all sorts of variations with no success.
How can I use the output to work out how the catch the exception that's been raised?