none
Can someone tell me why I get this Unhandled Exception Error?

    Question

  • The following code produces a Unhandled exception error that is basically like a general error. 

    Nothing shows in the message box for the error..the program just crashes

    Code Snippet

     

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
    {
        String^ sLocalFileName = "c:\\Users\\User\\Desktop\\dog.jpg";
        String^ sUrlPath = "http://imgsrv.kcbs.com/image/kcbs/UserFiles/Image/uno_clo.jpg";

        WebClient ^ wc    = gcnew WebClient ();
        Uri       ^ myUri = gcnew Uri       (sUrlPath);
        wc->DownloadFileCompleted   += gcnew AsyncCompletedEventHandler         (this,&Form1::myDownloadComplete);
        wc->DownloadFileAsync(myUri, sLocalFileName);
    }

    private: void myDownloadComplete(Object ^ sender, AsyncCompletedEventArgs ^ e)
    {
        MessageBox::Show(e->Error->Message->ToString());
    }

     

     

     

    This is the error I get

    Code Snippet

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

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

     

     

    Monday, February 18, 2008 2:35 PM

Answers

  •  Viorel. wrote:

    According to MSDN documentation for AsyncCompletedEventArgs.Error property, "The client application's event-handler delegate should check the Error property [...]; otherwise, the property will raise a TargetInvocationException [...]" [http://msdn2.microsoft.com/en-us/library/system.componentmodel.asynccompletedeventargs.error.aspx]. Therefore, you probably got the TargetInvocationException exception as a result of null Error value.

     

    Try additional checking with "if":

     

    Code Snippet


    void myDownloadComplete(Object ^ sender, AsyncCompletedEventArgs ^ e)

    {

        if( e->Error != null)

        {

            MessageBox::Show(e->Error->Message->ToString());

        }

        else

        {

            MessageBox::Show("Done.");

        }

    }

     

     

     

    I hope this makes sense.

     




    I origonally did that but to keep this example as simple as possible I omitted the test for nullptr.


    It turns out that I reinstaled windows and now the code works?  Weird but as long as it works im happy.

    Thanks everyone who helped
    Thursday, February 21, 2008 6:39 PM

All replies

  • TargetInvocationException hides actual exception catched by Windows Forms framework. Run the program under debugger, and see the following information in the Output window:

     

    First-chance exception of type ... in ...

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

    Open Debug - Exceptions window, find exception of the type mentioned in the "First-chance exception" message, and mark it as "Break when thrown". Then run the program again under debugger, and it will break in the place where exception occurs. This is general way of detecting exceptions hidden by TargetInvocationException.

     

    Looking at your code, I can see that you run MessageBox from arbitrary thread context, possibly this is the reason. You need to use Control.BeginInvoke and show MessageBox from main application thread.

    Monday, February 18, 2008 6:28 PM
  • unfortunately I am not that experienced with debugging exceptions
    I dont see any First-chance exception or anything related to that in the exceptions window.

    Ive played around with it for a couple of days and still cannot figure it out

    I am using .net 3.0

    Can anyone tell me why im getting this error?
    Wednesday, February 20, 2008 10:07 AM
  • First-chance exception message is in the Output window, when you run the program under Debugger. Having exception type, go to Exceptions window as described in my first post. Anyone doesn't know what is the reason of your program failure, you need to find this by breaking in the place where exception is thrown.

     

    Wednesday, February 20, 2008 11:53 AM
  • According to MSDN documentation for AsyncCompletedEventArgs.Error property, "The client application's event-handler delegate should check the Error property [...]; otherwise, the property will raise a TargetInvocationException [...]" [http://msdn2.microsoft.com/en-us/library/system.componentmodel.asynccompletedeventargs.error.aspx]. Therefore, you probably got the TargetInvocationException exception as a result of null Error value.

     

    Try additional checking with "if":

     

    Code Snippet


    void myDownloadComplete(Object ^ sender, AsyncCompletedEventArgs ^ e)

    {

        if( e->Error != null)

        {

            MessageBox::Show(e->Error->Message->ToString());

        }

        else

        {

            MessageBox::Show("Done.");

        }

    }

     

     

     

    I hope this makes sense.

     

    Wednesday, February 20, 2008 1:14 PM
  •  Alex Farber wrote:
    First-chance exception message is in the Output window, when you run the program under Debugger. Having exception type, go to Exceptions window as described in my first post. Anyone doesn't know what is the reason of your program failure, you need to find this by breaking in the place where exception is thrown.

     




    Now I understand what your saying.
    I have never had to debug exceptions before where the compiler doesnt stop where the error is.
    Now I can, thanks
    Thursday, February 21, 2008 6:38 PM
  •  Viorel. wrote:

    According to MSDN documentation for AsyncCompletedEventArgs.Error property, "The client application's event-handler delegate should check the Error property [...]; otherwise, the property will raise a TargetInvocationException [...]" [http://msdn2.microsoft.com/en-us/library/system.componentmodel.asynccompletedeventargs.error.aspx]. Therefore, you probably got the TargetInvocationException exception as a result of null Error value.

     

    Try additional checking with "if":

     

    Code Snippet


    void myDownloadComplete(Object ^ sender, AsyncCompletedEventArgs ^ e)

    {

        if( e->Error != null)

        {

            MessageBox::Show(e->Error->Message->ToString());

        }

        else

        {

            MessageBox::Show("Done.");

        }

    }

     

     

     

    I hope this makes sense.

     




    I origonally did that but to keep this example as simple as possible I omitted the test for nullptr.


    It turns out that I reinstaled windows and now the code works?  Weird but as long as it works im happy.

    Thanks everyone who helped
    Thursday, February 21, 2008 6:39 PM