1

我正在尝试在进入下一个表单之前处理登录错误,并且在大多数情况下,一切正常。我遇到了一个问题 - 当网络连接未建立时,ODBC 驱动程序会提供自己丑陋的错误消息。

这是我的代码:

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

所以,如果“other error message”被捕获,它会显示“fancy other error message”,但如果“ugly network problem message”被捕获,它会同时显示丑陋和花哨的错误消息。

我认为驱动程序本身正在生成一条消息,关于如何抑制它的任何想法?

4

1 回答 1

3

在处理异常之前,您需要处理该特定的 ODBCSystem.Exception异常。

要回答您有关具体细节的问题...

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

规则是您的异常处理必须从更多特定流向不太特定或从派生的异常类到它们的基类一直流回System.Exception. 换句话说,如果您有任何特定的异常要处理,则必须在处理异常之前处理这些System.Exception异常......

在您在块中放置断点时弹出的 Visual Studio 异常帮助程序中,Catch ex As Exception您可以准确地看到正在抛出的异常是什么,这是您必须在Catch ex As Exception块之前捕获的异常......

异常助手

正如您从屏幕截图中看到的,VS 异常助手显示异常的确切名称,如果您单击“查看详细信息”,您会看到一个包含更多详细信息的窗口,包括发生的异常的完整命名空间......你有没有看到这个异常帮助程序还是您正在使用其他 IDE?

于 2013-01-15T18:02:59.983 回答