Exception to exceptions

G

Guest

While developing the below code I noticed that exceptions were not working as
expected. An exception is thrown inside an XML import DLL and not being
caught inside the Main() code fragment, creating an unhandled exception
inside "theUI". Would anybody know why this is happening?

I thought the general rule for exceptions handling was the exception is
passed up the stack until the application handles it or the program
terminates?

Simplified code:-
--------------------------------------------------------------------------
Main code fragment
--------------------------------------------------------------------------
Form theUI = new Form();

try
{
if( theUI.ShowDialog() == DialogResult.OK )
{
// Success code
}
}
catch(Exception e)
{
// Exceptions catch all?
Error.ShowError("<Error message>");
return;
}

--------------------------------------------------------------------------
"theUI" code fragment
--------------------------------------------------------------------------

// UI button click event
private void Button_Click(object sender, System.EventArgs e)
{
string theDataFile = <wrong data file> // User selected wrong file
Import(theDataFile);
}

private void Import(string theDataFile)
{
Collection theCol = ImportTableXml(theDataFile); // Exception thrown
inside ImportTableXml()
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Are you using threads?

I just did a quick test , a win app with two forms, one call the other, the
second has a button and it raise an exception, it's correctly caught in the
first form:

//form 1:
private void button1_Click(object sender, System.EventArgs e)
{
try
{
new Form2().ShowDialog();
}
catch(Exception ee)
{
MessageBox.Show( ee.Message);
}
}


//form 2:
private void button1_Click(object sender, System.EventArgs e)
{
throw new Exception("text");
}


Cheers,
 
G

Guest

Hello Ignacio,

No I'm not using threading. I don't think you'll able to replicate it using
simple test code, "as it works like it says on the tin". In my example the
main code fragment is called from a (legacy) C++ EXE/DLL's through COM, which
I think maybe influencing the exception handling somehow? It's the first time
I've ever seen this behaviour.

My solution is to put the exception handling inside the dialog, which
unfortunately makes it a bit clunky.

Cheers,
Nick
 
G

Guest

Thanks Ollie,

I did think about doing this but I stopped in my tracks after debugging, I
will explain. When debugging I found that the exception does not get back to
the calling function (which is very strange). I get the .Net unhandled
exception message within the dialog with a option to continue, clicking on
continue the dialog code continues to execute normally.

Do you think this method would still handle this?

Cheers
 
O

Ollie Riches

Are you running with 'Break into the debugger' switched on for all
exceptions in visual studio?

Ollie Riches
 
G

Guest

No I wasn't, but I have tried this and it gives the same result.
Unfortunately the project dead line is looming at the end of this week and I
can not spend any more time on the matter. Also because of the nature of our
setup i.e. C++ main app calling down to .Net through COM, I think this
problem is going to be too difficult to diagnose remotely. I'm convinced its
something to do with .Net wrapped by COM. Eventually we'll move all the
legacy code into the .Net layer and this problem will disappear.

Many thanks
Nick
 

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