0

I've got a Windows Form with some wrong code inside (no matter what the errors are, they are on purpose to check the exceptions raised).

Remember I'm not using any try-catch, not inside the form, nor outside. I'm just expecting debugger throws exception and stops running.

When I show the form using ShowDialog(), exceptions are thrown with no problem. When I show the form using Show(), no exceptions are raised. The error occurs during code execution, the code interrupts running where exception should be raised, but form keeps active and functional, accepting clicks and running all events fine. Shouldn't the debugger throw the exceptions raised by the form???

Is that normal??? The Show() method was really meant not to throw exeptions??? Or is that some weird bug from the form being in an Autocad Plug-in???


Some code (I think it's useless, but there it goes)

public void ShowMyForm(MyForm MyFormInst)
{ MyFormInst.Show(); } //here, the form doesn't raise exceptions when I click button1.

public void ShowMyFormModal(MyForm MyFormInst)
{ MyFormInst.ShowDialog(); } //here, the form raises exceptions when I click button1.

class MyForm : Form
{
    //initialize and blablabla

    private void button1_Click(object sender, EventArgs e)    
    {
        double[] Arrr = new double[] {1, 2, 3};
        double Numb = Arrr[4]; //yes, did this on purpose to force exceptions.
    }
}
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214

1 Answers1

1

ShowDialog() stops the execution of the calling class/form. Show() does not. I would expect that you got a catch-block in the calling class/form where the Exception is handled and so not shown

example:

form1_click() {
  try {
  Form2 frm2=new Form2();
   frm2.Show();
  } catch {
   // Do Nothing
  }
}

Does not show you anything, because of the Catch in Form1

form1_click() {
  try {
  Form2 frm2=new Form2();
   frm2.ShowDialog();
  } catch {
   // Do Nothing
  }
}

Will show you the Exception in Form2, if there is no exception handling inside

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • I understand your point. In the first case, code continues after the Show and exceptions raised by form2 are simply not seen by form 1. But shouldn't I expect then that form2 would raise it's own exceptions instead of keep running as if they didn't occur?? (there's no `try-catch` inside form2). – Daniel Möller Apr 30 '13 at 14:44
  • 1
    It WILL raise its own exceptions, but you just don't see them. Put a breakpoint at the point where the exception occurs and step through the code to watch where the exception gets caught. (or Just use "Debug/Exceptions" and check the Checkbox on "Common Language Runtime Exceptions" on the "thrown" column – Ole Albers Apr 30 '13 at 15:01
  • Exception is not caught, that's what I'm saying. The debugger jumps from the exeption line directly to the form Idle state, waiting for user interactions. – Daniel Möller Apr 30 '13 at 15:05
  • You say it's normal for the form to treat the exeption itself? – Daniel Möller Apr 30 '13 at 15:10
  • (That debugger option worked, but it's surely a User-Nonhandled exception) – Daniel Möller Apr 30 '13 at 15:10
  • Perhaps that helps: http://stackoverflow.com/questions/6832338/what-causes-winforms-to-silently-discard-unhandled-exceptions-with-no-try-catch – Ole Albers Apr 30 '13 at 15:18