TargetInvocationException of BackgroundWorker_RunWorkerCompleted

Suppose the following situation. The form has a button that, when clicked, launches the background worker. The RunWorkerCompleted event handler has a piece of code that throws an exception without exception. The form is launched from the Application.Run method.

public partial class FormMain : Form
{
    public FormMain()
    {
        InitializeComponent();
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        throw new Exception();
    }

    private void button_Click(object sender, EventArgs e)
    {
        backgroundWorker.RunWorkerAsync();
    }
}

The problem is that Visual Studio is interrupted when calling Application.Run instead of "throw new Exception ()" in the FormMain.backgroundWorker_RunWorkerCompleted method. In addition, this real exception is wrapped with a TargetInvocationException, and the call stack is reduced to the Program.Main method and the code that caused the exception cannot be checked because of this.

How to prevent this packaging? Am I doing something inherently wrong?

, TargetInvocationException, , - .

EDIT: , InnerException TargetInvocationException , . , Visal Studio , TargetInvocationException, , VS IDE.

+3
1

, , RunWorkerCompleted . , , , - , , , , Application.Run(), .

, , . + , "" CLR. , Wheel Of Fortune. Application.SetUnhandledExceptionMode().

+3

All Articles