David,
Your try/catch in your Main is only catching exceptions that the constructor
of Form1 raises!
The exception that your button1_Click raises is not handled, VS.NET handles
the exception for you, so as to allow you to debug the problem with your
program.
You need to handle the Application.ThreadException event in your Main to
catch unhandled that button1_Click raises.
Depending on the type of application you are creating, .NET has three
different global exception handlers.
For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.
For console applications look at:
System.AppDomain.UnhandledException event
Use add handler in your Main.
For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use add handler in your Main.
It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.
There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...
http://msdn.microsoft.com/msdnmag/is...T/default.aspx
For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.
Note David has some excellent comments on argument validation to your class
libraries. Especially if those class libraries are going to be used outside
of your current solution.
Hope this helps
Jay
"David B" <David
B@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
Why is it so difficult to report bugs to Microsoft? I have a documented
bug
and an small test example. I don't really see why I should have to pay to
tell them about it...
Anyway, the following code exhibits the bug. If it is built and run from
Visual Studio.NET 2003 in debug mode it works correctly as expected.
However, if built in Release mode it fails with an "uncaught exception"
error, although the exception does have a valid catch handler.
To repeat, create a new Windows Forms application and put the following
code
into Form1.cs.
----
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.Run(new Form1());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Main: Caught exception: ");
}
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
Foo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "button1_Click: Caught exception: ");
throw;
}
}
void Foo()
{
throw new Exception("Exception thrown from Foo()");
}