Processing failing silently

J

John Hatpin

I'm new to C#, and I've been working on a casual project, learning as
I go, using SharpDevelop on WinXP, latest releases of everything.

Several times I've noticed that processing of a code block will
suddenly stop without any error, even with the debugger running, yet
the app continues.

For example, I have the following code in a class object's method,
which wasn't getting executed completely - I added in the messageboxes
to find out where processing was stopping:

[various statements ...]
this.List.Add(mGroupBox) ;
System.Windows.Forms.MessageBox.Show("adding control") ;
HostForm.Controls.Add(mGroupBox) ;
System.Windows.Forms.MessageBox.Show("control added") ;
mGroupBox.Text = CorrespondentName ;
[more statements ...]

Running that, I get the first messagebox but not the second, and
statements after the Controls.Add line don't get executed. Presumably,
there's something wrong with the Controls.Add line.

My question is not what's causing the code to fail - that would be a
separate question. My question is: is it normal for C# code to stop
executing like this, silently, with no indication that anything's
wrong apart from code unexpectedly not being executed?

It's certainly proving a pain to try to pin down errors like this, and
I've had this happen several times in different places, usually, it
seems, from unreferenced objects. The only sign I can see that
anything's wrong is the effect of that missed code later on, in
another part of the application.
 
R

Registered User

I'm new to C#, and I've been working on a casual project, learning as
I go, using SharpDevelop on WinXP, latest releases of everything.

Several times I've noticed that processing of a code block will
suddenly stop without any error, even with the debugger running, yet
the app continues.

For example, I have the following code in a class object's method,
which wasn't getting executed completely - I added in the messageboxes
to find out where processing was stopping:

[various statements ...]
this.List.Add(mGroupBox) ;

Here the mGroupBox object is added to a container of some sort.
System.Windows.Forms.MessageBox.Show("adding control") ;
HostForm.Controls.Add(mGroupBox) ;

Here the same mGroupBox object is added to a Controls collection. That
is the cause of the unexpected behavior.
System.Windows.Forms.MessageBox.Show("control added") ;
mGroupBox.Text = CorrespondentName ;
[more statements ...]

Running that, I get the first messagebox but not the second, and
statements after the Controls.Add line don't get executed. Presumably,
there's something wrong with the Controls.Add line.

My question is not what's causing the code to fail - that would be a
separate question. My question is: is it normal for C# code to stop
executing like this, silently, with no indication that anything's
wrong apart from code unexpectedly not being executed?

Somewhere the exception is being handled. Exactly where can't be
determined by the provided information.
It's certainly proving a pain to try to pin down errors like this, and
I've had this happen several times in different places, usually, it
seems, from unreferenced objects. The only sign I can see that
anything's wrong is the effect of that missed code later on, in
another part of the application.

It shouldn't be a problem. Just enclose the code in a try-catch block
and inspect any exception that is handled. After catching the
exception stepping through the code may reveal where the exception is
currently being handled.

try
{
[various statements ...]
this.List.Add(mGroupBox) ;
System.Windows.Forms.MessageBox.Show("adding control") ;
HostForm.Controls.Add(mGroupBox) ;
System.Windows.Forms.MessageBox.Show("control added") ;
mGroupBox.Text = CorrespondentName ;
[more statements ...]
}
catch(Exception ex)
{
// inspect exception here
}

regards
A.G.
 
J

John Hatpin

Registered said:
It shouldn't be a problem. Just enclose the code in a try-catch block
and inspect any exception that is handled. After catching the
exception stepping through the code may reveal where the exception is
currently being handled.

Thanks to both of you for your replies. They've been very useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top