4

Trying to ask my question again - last time it messed up.

This is my example code:

  1. Little form, that contains only button and combobox:

    public partial class question : Form
    {
    public question()
    {
        InitializeComponent();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        comboBox1.DataSource = new List<string>() { "a", "b", "c" };
    }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show("In comboBox1_SelectedIndexChanged");
        throw new Exception();
    }
    }
    
  2. Project's Program class that calls the question form and handles the exceptions:

     class Program
    {
    static void Main(string[] args)
    {
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
    
            // Add the event handler for handling UI thread exceptions to the event.
            Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod);
    
            // Set the unhandled exception mode to force all Windows Forms errors to go through
            // our handler.
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    
            // Add the event handler for handling non-UI thread exceptions to the event. 
            AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
            Application.Run(new question());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message + "3");
        }
    }
    private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
    {
        Console.WriteLine(t.Exception.Message + "1");
    }
    
    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Console.WriteLine(((Exception)e.ExceptionObject).Message + "2");
    }
    }
    

Now, when one clicks the button, the event "SelectedIndexChanged" is rise (and the messageBox appear) but the exception is ignored. Only when the user selects manually another index in the combobox, the exception handled.

The question is, how to handle these exceptions too?

Cleptus
  • 3,446
  • 4
  • 28
  • 34
Yehezkel
  • 125
  • 1
  • 8
  • Let me get this right. Your console based application and winForm are two separate applications and you want to run the winFrom application from console based application - is that right? – Yawar Murtaza Feb 28 '17 at 10:43
  • @YawarMurtaza thanks. Isn't two separated application. It's just that the Main of the application is console application (actually is always like that). Not really matter. – Yehezkel Feb 28 '17 at 10:51
  • Okay - I have created the exact copy of your code. The runtime never reaches " Console.WriteLine(ex.Message + "3");" statement no matter what is done in the winForm. There must be something to do with Application.Run() method, it creates a new message loop for the winForm application on the same thread on which the Main method runs. Let me see if I can find how the exceptions are handelled in this case. – Yawar Murtaza Feb 28 '17 at 11:03
  • It seems like an old exception handling problem:http://stackoverflow.com/questions/1583351/silent-failures-in-c-seemingly-unhandled-exceptions-that-does-not-crash-the-pr – Tomas Chabada Feb 28 '17 at 11:18

1 Answers1

4

could not intercept exception even with try-catch:

try
{
    comboBox1.DataSource = new List<string>() {"a", "b", "c"};
}
catch (Exception ex)
{
}

It seems ComboBox ignores exceptions which happen when DataSource is set:

ListControl.DataSource source code

try 
{
    SetDataConnection(value, displayMember, false);
} catch 
{
    DisplayMember = "";
}

ComboBox.DataSource uses base ListControl implementation

ASh
  • 34,632
  • 9
  • 60
  • 82