调用目标已抛出 VB.Net 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38070996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:58:23  来源:igfitidea点击:

VB.Net Exception has been thrown by the target of an invocation

vb.net

提问by SilverShotBee

I am using the following code in VB.Net (Winforms) to simply loop through a DataGridViewand hide rows that are not needed.

我在 VB.Net (Winforms) 中使用以下代码来简单地循环遍历 aDataGridView并隐藏不需要的行。

Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Overview_Workstream_Sort.SelectedIndexChanged

    For Each row In Incident_Persons_List.Rows
        If Incident_Persons_List.Rows(CInt(row)).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) Then
            Debug.Print("User found in workstream")
            Incident_Persons_List.Rows(CInt(row)).Visible = True
        Else
            Incident_Persons_List.Rows(CInt(row)).Visible = False
        End If
    Next

End Sub

When the debugger gets to the first line of the IFstatement, I get the following error:

当调试器到达IF语句的第一行时,出现以下错误:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

mscorlib.dll 中发生类型为“System.Reflection.TargetInvocationException”的未处理异常

附加信息:调用的目标已抛出异常。

I've been trying everything I can think of to understand why this is. I've looked up the fault, but everyone seems to have completely different issues when this exception is thrown.

我一直在尝试我能想到的一切来理解为什么会这样。我已经查过错误,但是当抛出这个异常时,每个人似乎都有完全不同的问题。

Is it something to do with how I am doing a compare?

这与我进行比较的方式有关吗?

UPDATE 1

更新 1

  1. I have removed the For Eachand replaced it with For i = 0 to Incident_Persons_list.Rows.Count
  2. I have removed the Cintinstruction
  3. The Try/Catchas revealed that the actual exception being thrown is:
  1. 我已经删除并将其For Each替换为For i = 0 to Incident_Persons_list.Rows.Count
  2. 我已经删除了Cint指令
  3. Try/Catch为透露,被抛出的实际的例外是:

Row associated with the currency manager's position cannot be made invisible.

与货币经理职位相关的行不能隐藏。

UPDATE 2

更新 2

Everything is now working correctly with the below code:

现在一切正常,使用以下代码:

 Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Overview_Workstream_Sort.SelectedIndexChanged
        Try
            For i = 0 To Incident_Persons_List.Rows.Count - 1
                If Incident_Persons_List.Rows(i).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) Then
                    Debug.Print("User found in workstream")
                    Incident_Persons_List.Rows(i).Visible = True
                Else


                    'Your code that will throw the Exception
                    Incident_Persons_List.CurrentCell = Nothing
                    Incident_Persons_List.Rows(i).Visible = False

            End If
        Next
        Catch ex As TargetInvocationException
            'We only catch this one, so you can catch other exception later on
            'We get the inner exception because ex is not helpfull
            Dim iEX = ex.InnerException
            Debug.Print(iEX.Message)
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub

Thanks for the help!

谢谢您的帮助!

回答by Marco Guignard

As your row variable is an enumerated element of Incident_Persons_List.Rows and not an index of the element in the collection I think you should replace.

由于您的行变量是 Incident_Persons_List.Rows 的枚举元素,而不是集合中元素的索引,因此我认为您应该替换。

Incident_Persons_List.Rows(CInt(row))

By

经过

row

Or using a basic for structure instead of foreach. Somethink like

或者使用基本的结构而不是 foreach。有点像

For row = 0 To Incident_Persons_List.Rows.Count - 1 Step 1
   //SomeStuff
Next

回答by Martin Verjans

TargetInvocationException:

目标调用异常

The exception that is thrown by methods invoked through reflection

通过反射调用的方法抛出的异常

How to find out what's going on (because that exception is not really helpfull) ?

如何找出发生了什么(因为该异常并没有真正的帮助)?

You must surroung the calling block with a Try/Catchstructure, and then examine the InnerExceptioncaught :

您必须用Try/Catch结构包围调用块,然后检查InnerException捕获的:

Try
    'Your code that will throw the Exception
Catch ex As TargetInvocationException
    'We only catch this one, so you can catch other exception later on
    'We get the inner exception because ex is not helpfull
    Dim iEX = ex.InnerException
    'Now you can do some stuff to handle your exception
Catch ex As Exception
    'Here you catch other kinds of Exceptions that could occur in your code, if you want to...
End Try

And depending on the InnerException, you can now correct your code.

根据 InnerException,您现在可以更正您的代码。