TRY..CATCH block not working as expected

  • Thread starter Thread starter ECathell
  • Start date Start date
E

ECathell

I am having a problem with my try..catch blocks...

I push a button on my form.

using SQLDMO

I verify the status of the server. If it is off I turn it on.
I then run a query that requires a service restart.
I stop the SQL Agent
I stop the SQL Server which throws an error.

even though I have try..catch in every procedure block, the error falls through all the way to the button push event. WHY?
 
ECathell,
Without seeing how you set up your try..catch blocks its hard to answer your
question.

Can you give a complete example (15 to 20 lines) of your code that shows
where the exception is happening & where its being caught.

Thanks
Jay

I am having a problem with my try..catch blocks...

I push a button on my form.

using SQLDMO

I verify the status of the server. If it is off I turn it on.
I then run a query that requires a service restart.
I stop the SQL Agent
I stop the SQL Server which throws an error.

even though I have try..catch in every procedure block, the error falls
through all the way to the button push event. WHY?
 
Let me know if this is what you need.





Imports System.IO

Imports vb = Microsoft.VisualBasic

Imports System.Data.SqlClient

Imports Microsoft

Imports SQLDMO





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Cursor.Current = Cursors.WaitCursor

Dim curSQLServer As SQLServer

Try

Dim i, x As Integer



'For i = 0 To Me.ListBox1.Items.Count - 1

'Dim curComputer As String = Me.ListBox1.Items.Item(i)

Dim curComputer As String = "eric-cathell"



Me.sbpServerName.Text = curComputer



'------------------------------------------------------------------------------------------------

'Initialize a new instance of SQLserver for computer.

'------------------------------------------------------------------------------------------------



curSQLServer = initSQLServer(curSQLServer, curComputer)

'------------------------------------------------------------------------------------------------

'Determine if SQL server is running on this computer.

'------------------------------------------------------------------------------------------------

If Not curSQLServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running Then

curSQLServer = StartSQLServer(curSQLServer)

End If

wait(5)

Dim sproc As New SQLProcedures(curComputer)

Dim s As String = sproc.getServerName(s)

'If not (UCase(s) = UCase(curComputer)) Then

'Me.sbpServerName.Text += "-" & s

sproc.fixServername(curComputer, s)

Me.sbpServerName.Text = curComputer

curSQLServer = StopServer(curSQLServer)

wait(5)



curSQLServer = StartSQLServer(curSQLServer)

Me.sbpCurrentAction.Text = "done"



'End If



'Next

Cursor.Current = Cursors.Default



Catch ex As Exception

MessageBox.Show("userInterface.vb.Button1_Click", ex.Message,
ex.Source, ex.StackTrace)

Finally



End Try

End Sub



'========================================================================================

'DESCRIPTION: This procedure

'CONSTRUCTED BY Eric E Cathell,

'DATE: 02/17/2005,

'PROCEDURE TYPE/RETURN TYPE:

'ACTIVE PROJECT: ScaleRenamer

'ACTIVE MODULE: userInterface.vb

'ACTIVE CLASS: executeChanges

'ACTIVE PROCEDURE: executeChanges

'

'ARGUMENTS:' <no arguments>

'========================================================================================

Private Function StartSQLServer(ByVal srv As SQLServer) As SQLServer



Try



Select Case srv.Status

Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Paused

srv.Continue()

Do Until srv.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running

Loop

Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running

Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped

srv.Start(False)

Do Until srv.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running

Loop

End Select

wait(5)

srv.Connect()

Select Case srv.JobServer.Status

Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Paused

Me.sbpCurrentAction.Text = "Start SQL Agent"

srv.JobServer.Start()

Do Until srv.JobServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running

Loop

Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running

MessageBox.Show("SQlAgent is already Running")



Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped

Me.sbpCurrentAction.Text = "Start SQL Agent"

srv.JobServer.Start()

Do Until srv.JobServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running

Loop

End Select

Return srv



Catch ex As Exception

MessageBox.Show(ex.Message, "Exception Thrown")

Finally



End Try

End Function

Private Function StopServer(ByVal srv As SQLServer) As SQLServer

Try

srv.Connect()

Select Case srv.JobServer.Status

Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Paused

Me.sbpCurrentAction.Text = "Stop SQL Agent"

srv.JobServer.Stop()

Do Until srv.JobServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped

Loop

Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Running

Me.sbpCurrentAction.Text = "Stop SQL Agent"

srv.JobServer.Stop()

Do Until srv.JobServer.Status =
SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped

Loop

Case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped

MessageBox.Show("SQlAgent is already Stopped")

End Select

wait(5)

Debug.WriteLine(srv.Status.ToString)

'srv.DisConnect()

srv.Shutdown()



Catch ex As System.Runtime.InteropServices.COMException

'MessageBox.Show(ex.Message, ex.InnerException.ToString)

srv.DisConnect()

Return srv

Catch ex As Exception

MessageBox.Show(ex.Message, ex.InnerException.ToString)

Return srv

Finally



End Try

End Function



Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.DoubleClick

Me.ListBox1.Items.Remove(ListBox1.SelectedItem)

End Sub



Private Function initSQLServer(ByVal srv As SQLServer, ByVal name As
String) As SQLServer

Try



If Not srv Is Nothing Then

srv.DisConnect()

srv = Nothing

srv = New SQLDMO.SQLServer

Else

srv = New SQLDMO.SQLServer



End If



With srv

.LoginTimeout = 10

.Name = name

.Login =

.Password =

End With

Return srv





Catch ex As Exception

MessageBox.Show(ex.Message, "Exception Thrown")

Finally



End Try

End Function

Private Sub wait(ByVal time As Integer)

Dim t As String

t = time & "0000"



time = CInt(t)



Dim i As Integer



For i = 0 To time

Me.sbpTimer.Text = i



Next

End Sub



End Class
 
Eric,
The code helps, now we need an explanation of where the exception is
happening & where its being caught...

Normally when I display exceptions I use Exception.ToString so as to include
the call stack & every thing:
MessageBox.Show(ex.ToString(), "Exception Thrown")

Especially in debug builds.

Thanks
Jay
 
If the error is truly occurring in the StopServer function then I would
guess that there is an issue with your srv object at the time you run
srv.Disconnect which is throwing a NEW exception that's being caught by
the try..catch block in the button click event.

Hope this helps,
Brian Swanson
 
Brian,
Looking at it again: I suspect you are right, the call stack on the
exception should let Eric know for certain...

Jay
 
Thanks for the information, I will give it a shot and see what it brings
about...I have left it go for now as it has been a frustrating bit of
coding...hehe

thank you for the tip on ex.string...that will be helpful in all my code..
 
Back
Top