In C #, WinForms, VS2008, .NET 3.5 ...
For this code:
static class Program
{
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormThatDividesByZero());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public partial class FormThatDividesByZero : Form
{
public FormThatDividesByZero()
{
InitializeComponent();
}
private void DivideByZeroButton_Click(object sender, EventArgs e)
{
int a = 0;
int b = 0;
int c = a / b;
}
}
Full source: http://forgefx.com/posts/ExceptionReporting.zip
When I start this small test project, through F5, from the development environment, the exception after clicking the DivideByZero button breaks and the message box starts. When I start the project by double-clicking the .exe file in the / bin / Debug folder, the exception is not caught and there is no message box - why is this?
When .exe is launched from outside the IDE or with "Debug> Start Without Debugging" from Visual Studio, I get an unhandled exception. My understanding was that the code above would catch all the exceptions.