473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

TRY..CATCH block not working as expected

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?

--
--Eric Cathell, MCSA
Nov 21 '05 #1
6 4132
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

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:eJ*************@TK2MSFTNGP15.phx.gbl...
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?

--
--Eric Cathell, MCSA
Nov 21 '05 #2
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


Nov 21 '05 #3
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

"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:u0**************@tk2msftngp13.phx.gbl... Let me know if this is what you need.

<<code sample removed>>

Nov 21 '05 #4
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
"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
Let me know if this is what you need.
Private Function StopServer(ByVal srv As SQLServer) As SQLServer

Try

srv.Connect()

<CODE REMOVED>

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


Nov 21 '05 #5
Brian,
Looking at it again: I suspect you are right, the call stack on the
exception should let Eric know for certain...

Jay

"Brian Swanson" <pu*******@gREMOVETHISmail.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
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
"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
Let me know if this is what you need.
Private Function StopServer(ByVal srv As SQLServer) As SQLServer

Try

srv.Connect()


<CODE REMOVED>

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

Nov 21 '05 #6
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..
--
--Eric Cathell, MCSA
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eC****************@TK2MSFTNGP10.phx.gbl...
Brian,
Looking at it again: I suspect you are right, the call stack on the
exception should let Eric know for certain...

Jay

"Brian Swanson" <pu*******@gREMOVETHISmail.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
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
"ECathell" <ec******@nospam.mountaire.com> wrote in message
news:ec******@nospam.mountaire.com:
Let me know if this is what you need.
Private Function StopServer(ByVal srv As SQLServer) As SQLServer

Try

srv.Connect()


<CODE REMOVED>

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


Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Travis | last post by:
I am trying to prevent an exception from bubbling up from one nested Try/Catch Block to its "Parent" Try/Catch Block. Here is some example code: try{ try{ //Non-application killing code that...
2
by: Greg Merideth | last post by:
This little bugger of a problem took us most of the day to track down but I'm glad it happened as I would like to know why the WSDL generated code that operates inside of a try..catch block throws...
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
2
by: iftekhar | last post by:
hi there , consider the followinf code #include <iostream> #include <stdexcept> using namespace std; int main (void) {
4
by: garyusenet | last post by:
Hi I'm using the following code which is finally working. Public Class Form1 Shared ActElement As Object Shared ActFields As DataSet Public Sub SetActElement() Dim objApp As New Object
15
by: Neo | last post by:
Hello All, Although, I have read all the advantages of using Try Catch Block instead of "On error goto", I am still confused what is alternative for classic "Resume" statement. "Resume" was one...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
4
by: cj | last post by:
my old code Try Dim sw As New System.io.StreamWriter(fileName, True) sw.WriteLine(strToWrite) sw.Close() Catch End Try my new code
16
by: HillBilly | last post by:
This is freaking me out. I'm using Membership and trying to determine if the database is online. The GetConnectionString( ) method returns a connection string as expected but not when used in the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.