捕捉为对象帮助 [英] Catch As Object Help

查看:73
本文介绍了捕捉为对象帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获对象,但出现错误
错误:

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

推荐答案

您只能捕获异常-它们是导致捕获首先发生的原因,并且所有这些异常都必须派生自System.Exception

另外,您不应该在代码尝试执行操作时就忽略异常-抛出异常是有原因的,应该调查或至少记录该原因.
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.


这篇关于捕捉为对象帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆