Catching Errors with try catch

G

gv

Hi all,

I'm trying to catch errors and when running in VS.net 2003 my try catch
doesn't seam to catch the error?

this error comes up: why does this not catch it?
Catch ex As System.Data.SqlClient.SqlException

"A first chance exception of type 'System.Data.SqlClient.SqlException'
occurred in system.data.dll
Additional information: System error."

my code is this:

Public Function RunSQLcmd(ByVal SQLquery As String, ByVal Msgtxt As String)

Dim result As String = String.Empty
Dim MessageFrm As StatMess = New StatMess
MessageFrm.StatusMessage(Msgtxt)

Try
' Reset the cursor to the hourglass postion.
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
SQLConnection(loginDatabase, loginIPaddress, "Standard",
loginuser, loginpass)
Dim MyCmd2 As New SqlCommand(SQLquery)
MyCmd2.CommandTimeout = 600
MyCmd2.Connection = SQLConn
SQLConn.Open()
MyCmd2.ExecuteNonQuery()
SQLConn.Close()
Application.DoEvents()

Catch ex As System.Data.SqlClient.SqlException
result = "There was an SqlException.." & vbCrLf & "Details..." &
vbCrLf & ex.ToString()

Catch ex As InvalidCastException
result = "There was an InvalidCastException..." & vbCrLf &
"Details..." & vbCrLf & ex.ToString()

Catch ex As Exception
result = "There was an Unhandled Exception..." & vbCrLf &
"Details..." & vbCrLf & ex.ToString()
Finally


If IsNothing(MessageFrm) = False Then MessageFrm.Dispose()
If IsNothing(SQLConn) = False Then
If SQLConn.State <> Data.ConnectionState.Closed Then
SQLConn.Close() : SQLConn.Dispose()
End If
MsgBox(result)

Cursor.Current = System.Windows.Forms.Cursors.Default
MessageFrm.close()

End Try

' Reset the cursor to the default for all controls.
Cursor.Current = System.Windows.Forms.Cursors.Default
SQLConn.Close()
MessageFrm.close()

End Function

thanks
Gerry
 
G

gv

Thanks for replying.

this line:
MyCmd2.ExecuteNonQuery()

I know the problem is in the sql query that I pass to this function, I did
this on purpose

to test the try catch.

thanks

Gerry
 
W

W.G. Ryan eMVP

Hmm, if the query is bad than it should be caught by the SqlException -
unless for instace you have a null reference or something but that doesn't
look like it could be the case from this code. For what it's worth, I've
used the idential code and just configured it to hit a db on my machine -
it's working as expected in both regards. Let me play with it some more and
see if I can find anything.
 
G

gv

Thanks for your help,

I just found what the problem was. I had options under Exceptions under
debug tab in VS set to break into the debugger. Sorry!!!

Seams to work now.

thanks again
Gerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top