Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to catch an object but I get errors
Errors:

Error	3	'Catch' cannot catch type 'Object' because it is not 'System.Exception' or a class that inherits from 'System.Exception'.	D:\frm.vb	66	35


Private Sub TmbEndService_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim flag As Boolean = False
    Dim item As ListViewItem
    For Each item In Me.ListViewServices.CheckedItems
        Application.DoEvents
        Try
            Dim services As ServiceController() = ServiceController.GetServices
            Dim i As Integer
            For i = 0 To services.Length - 1
                Try
                    Do While (item.Text = services(i).ServiceName.Trim)
                        Application.DoEvents
                        If (services(i).Status.ToString = "Stopped") Then
                            MessageBox.Show("The service(s) status stopped", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                            Me.TmbRefResh_Click(sender, e)
                            Application.DoEvents
                        ElseIf services(i).CanStop Then
                            services(i).Stop
                            Thread.Sleep(&H3E8)
                            item.Remove
                            Application.DoEvents
                        Else
                            MessageBox.Show("The service(s) can't be stop", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                        End If
                    Loop
                Catch obj1 As Object
                End Try
                services(i).Refresh
            Next i
            flag = True
        Catch obj2 As Object
        End Try
    Next
    If Not flag Then
        MessageBox.Show("Please select service(s) do you want to end", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Me.Refresh
    End If
End Sub
Posted

1 solution

You can only catch exceptions - they are what causes the catch to occur in the first place, and all of them must be derived from System.Exception

In addition, you shouldn't just ignore exceptions as your code is trying to do - they are thrown for a reason, and that reason should be investigated or at least logged.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900