调用目标已抛出 VB.Net 异常 [英] VB.Net Exception has been thrown by the target of an invocation

查看:45
本文介绍了调用目标已抛出 VB.Net 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) 处理Overview_Workstream_Sort.SelectedIndexChanged对于 Incident_Persons_List.Rows 中的每一行If Incident_Persons_List.Rows(CInt(row)).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) 然后Debug.Print("用户在工作流中找到")Incident_Persons_List.Rows(CInt(row)).Visible = True别的Incident_Persons_List.Rows(CInt(row)).Visible = False万一下一个结束子

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

<块引用>

未处理的异常类型mscorlib.dll 中出现System.Reflection.TargetInvocationException"

附加信息:异常已被一个目标抛出调用.

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

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

更新 1

  1. 我删除了 For Each 并将其替换为 For i = 0 to Incident_Persons_list.Rows.Count
  2. 我已经删除了 Cint 指令
  3. Try/Catch 显示实际抛出的异常是:

<块引用>

不能制作与货币经理职位相关的行看不见.

更新 2

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

 Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) 处理 Overview_Workstream_Sort.SelectedIndexChanged尝试对于 i = 0 到 Incident_Persons_List.Rows.Count - 1如果 Incident_Persons_List.Rows(i).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) 那么Debug.Print("用户在工作流中找到")Incident_Persons_List.Rows(i).Visible = True别的'你的代码会抛出异常Incident_Persons_List.CurrentCell = 无Incident_Persons_List.Rows(i).Visible = False万一下一个捕获 ex 作为 TargetInvocationException'我们只捕捉这个,所以你可以稍后捕捉其他异常'我们得到内部异常,因为 ex 没有帮助Dim iEX = ex.InnerExceptionDebug.Print(iEX.Message)Catch ex 作为例外Debug.Print(ex.Message)结束尝试结束子

感谢您的帮助!

解决方案

TargetInvocationException :

<块引用>

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

如何找出正在发生的事情(因为该异常并没有真正的帮助)?

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

试试'你的代码会抛出异常捕获 ex 作为 TargetInvocationException'我们只捕捉这个,所以你可以稍后捕捉其他异常'我们得到内部异常,因为 ex 没有帮助Dim iEX = ex.InnerException'现在你可以做一些事情来处理你的异常Catch ex 作为例外'在这里,您可以捕获代码中可能发生的其他类型的异常,如果您想...结束尝试

并且根据 InnerException,您现在可以更正您的代码.

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

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 IF statement, I get the following error:

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

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

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. I have removed the For Each and replaced it with For i = 0 to Incident_Persons_list.Rows.Count
  2. I have removed the Cint instruction
  3. The Try/Catch as revealed that the actual exception being thrown is:

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

UPDATE 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!

解决方案

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/Catch structure, and then examine the InnerException caught :

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.

这篇关于调用目标已抛出 VB.Net 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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