My database connection routine can fail, for example, if incorrect credentials are supplied. In this case, a general OleDbException is thrown, with an ErrorCode of -2147217843. The message gives an error such as
No error message available, result code:
DB_SEC_E_AUTH_FAILED(0x80040E4D)
How do I catch this? Using the error code -2147217843 seems a bit "magic numbers".
Try
mCon = New OleDbConnection(connectionString)
mCon.Open()
Catch ex As OleDbException
If ex.ErrorCode = -2147217843 Then
Engine.logger.Fatal("Invalid username and/or password", ex)
Throw
End If
Catch ex As Exception
Engine.logger.Fatal("Could not connect to the database", ex)
Throw
End Try