Question

I am using a third party software for control proxy in my system.
so every network connection uses that proxy software.
in my windows application i have some codes for upload my files through FTP.
i have many try-catch around ftp class and in main application for prevent showing any error and stop the application.
when that proxy software is run and that ftp class is trying to connect to my server and suddenly i close that proxy software my program is closed for unknown reason.
how can i prevet that program to close on crash and keep it alive?
why those tr-catch can not do their job?

EDIT 1 :
those catch parts never run to get the error.
please see this pic :

IMAGE 1

IMAGE 2

but normal exception window is like this :

IMAGE 3

EDIT 2 :
and my try/catch is like below:

class My_FTP
{
    public void upload()
    {
      try
      {
        //Codes

        CRASH IS HERE AND WE NEVER JUMP TO CATCH EREA!!!!!!!!!!!!!!!!!!!!

        while (bytesSent != 0)
       {

       }
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }
    }
}


private void Form1_Load(object sender, EventArgs e)
{
  try
  {
    My_FTP my_ftp_ = new My_FTP();
    my_ftp_.upload();
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.ToString());
  }

  try
  {
    //Other_Codes;
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.ToString());
  }

}    

i tried these :

Thread: "vshost32-clr2.exe has stopped working" -> not programmatically

VShost32.exe stopped working, but I can continue debugging -> no help

Was it helpful?

Solution

it may be because of native code in third party library and you cannot catch native Exception in this way, i have faced similar problem when using LibVlc in C#,

you may find some help here to catch native exception (http://msdn.microsoft.com/en-us/magazine/dd419661.aspx)

similar problem Can you catch a native exception in C# code?

also try to remove (Exception ex) from catch statement

OTHER TIPS

Perhaps you should add some kind of logging for when you catch an exception. The reason you program is closing is because when the exception is thrown, the method is being exited, causing your Main method to exit.

In your try catch block try putting some logic within the catch for example:

catch(Exception e)
{
   MessageBox.Show("Error: " + e.Message, "Exception Thrown", MessageBoxButtons.OK, MessageBoxIcon.Error);
   OtherMethod(); //Or something like this
}

Obviously there will be better ways to go about this, I am only providing a simple solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top