The example I'm using is right out of the Que Certification book: "MCAD/MCSD
Developing and Implementing Windows-based Applications with Visual C# .NET
and Visual Studio .NET" for Exam 70-316, Ch. 3, Step By Step 3.5, "Logging
Unhandled Exceptions in the Windows Event Log".
Environment:
Microsoft Development Environment 2003 v7.1.6030
Microsoft .NET Framework 1.1 v1.1.4322
Windows Vista
My code matches the example files included with the book's CD, and neither
ever gets to the created UnhandledExceptionHandler(). Another example I
pulled from Microsoft documentation, where the error is thrown manually (via
'throw'), does get to the handler. So the difference is that one purposely
throws it, which does get there, and the book's example relies on a real
error (e.g., a divide-by-zero exception), which doesn't get there.
3 TextBox: txtMiles, txtGallons, txtEfficiency; 1 Button: btnCalculate
1 'using' in addition to those automatically supplied by the designer: using
System.Diagnostics;
Start Code
=====
[STAThread]
static void Main()
{
AppDomain adCurrent = AppDomain.CurrentDomain;
adCurrent.UnhandledException += new
UnhandledExceptionEventHandler(UnhandledExceptionHandler);
Application.Run(new StepByStep3_05());
}
private static void UnhandledExceptionHandler(object sender,
UnhandledExceptionEventArgs ue)
{
Exception unhandledException = (Exception) ue.ExceptionObject;
if (!EventLog.SourceExists("Mileage Efficiency Calculator"))
{
EventLog.CreateEventSource("Mileage Efficiency Calculator",
"Mileage Efficiency Calculator Log");
}
EventLog eventLog = new EventLog();
eventLog.Source = "Mileage Efficiency Calculator";
eventLog.WriteEntry(unhandledException.Message);
MessageBox.Show("An exception occurred: Created an entry in the log file");
}
private void btnCalculate_Click(object sender, System.EventArgs e)
{
decimal decMiles = Convert.ToDecimal(txtMiles.Text);
decimal decGallons = Convert.ToDecimal(txtGallons.Text);
decimal decEfficiency = (decMiles / decGallons);
txtEfficiency.Text = String.Format("{0:n}", decEfficiency);
}
=====
End Code
I do not care whether it records the error in an event log or not. I'd take
anything just to see a message box from UnhandledExceptionHandler() that
shows it got there. When anything is amiss durring btnCalculate(), it shows
the standard debugging dialog, but never gets to my event handler, whether
or not I choose Debug or Continue. The book expects I'll see my own message
box when I run it within the development environment and purposely enter
something wrong on the form.
What gives?