abarrington
abarrington

Reputation: 166

Try/Catch is not suppressing ODBC driver error message

I'm attempting to handle login errors before advancing to the next form and, for the most part, everything is working. I am having one problem - when the network connection is not established, the ODBC driver provides its own ugly error message.

Here's my code:

Try
     ODBCconnection.Open()
     ODBCconnection.Close()
     Me.Hide()
     otherForm.ShowDialog()
Catch ex As Exception
     If ex.Message.IndexOf("ugly network problem message") > -1 Then
         MsgBox("fancy network problem message")
     ElseIf ex.Message.IndexOf("other error message") > -1 Then
         MsgBox("fancy other error message")
     End If
End Try

So, if "other error message" is caught, it shows "fancy other error message", but if "ugly network problem message" is caught, it shows both the ugly and the fancy error messages.

My thought it that the driver itself is generating a message, any ideas on how to suppress it?

Upvotes: 1

Views: 2376

Answers (1)

Dean Kuga
Dean Kuga

Reputation: 12119

You will need to handle that specific ODBC exception before handling the System.Exception exception.

To Answer your question about the specifics...

Try
     ODBCconnection.Open()
     ODBCconnection.Close()
     Me.Hide()
     otherForm.ShowDialog()
Catch oex as System.Data.Odbc.OdbcException
     'Do something with the OdbcException
Catch ex As Exception
     If ex.Message.IndexOf("ugly network problem message") > -1 Then
         MsgBox("fancy network problem message")
     ElseIf ex.Message.IndexOf("other error message") > -1 Then
         MsgBox("fancy other error message")
     End If
End Try

The rule is that your exception handling must flow from more to less specific or from derived Exception classes to their base classes all the way back to System.Exception. In other words, if you have any specific exceptions to handle you must handle those before you handle the System.Exception exception...

In Visual Studio exception helper that pops up when you place a break point in your Catch ex As Exception block you can see exactly what is the exception that is being thrown and that is the exception that you must catch before your Catch ex As Exception block...

Exception Helper

As you can see from the screenshot the VS exception helper displays the exact name of the Exception and if you click on 'View Detail' you get a window with many more details including the full namespace of the exception that occurred... have you ever seen this Exception Helper or are you using some other IDE?

Upvotes: 3

Related Questions