My UnhandledExceptionHandler Won't Fire

V

victorcamp

I'm new to Visual Studior C# .Net 2003 on Vista, and have been uable to get
my UnhandledExceptionHandler() to fire.

I have added these lines to Main() before the Application.Run():
AppDomain adCurrent = AppDomain.CurrentDomain;
adCurrent.UnhandledException += new
UnhandledExceptionEventHandler(UnhandledExceptionHandler);

It kicks out the regular debugging error, but never gets to my own event
handler. (I'm trying to learn exception handling, so I'm purposely not
handling the error at the moment.)

Is there a 'using' statement or a setting I should be using? There are no
compile errors.
 
G

Guest

Victor,
This is most likely because you are doing your testing in the Visual Studio
IDE in debug mode, where the behavior is much different than when your
applicaition is running as a release - built app outside of the debugger
environment.
Hope that makes sense.
Peter
 
V

victorcamp

I still have only a small understanding of the Visual Studio environment.

I tried just double-clicking the .exe, but that didn't fire it. The error
that did come up then suggested turning on JIT in the config with
<system.windows.forms jitDebugging="true" />, but there are lots of .config
files to chose from. I chose devenv.exe.config, but that didn't change
anything either.

I'm following through exercises in the Microsoft certification training
book, and it says I should be able to raise it right there in the Visual
Studio environment. I also tried the example exercises included with the
book, and they didn't fire it either. Without the ability to see my own
UnhandledExceptionHandler fire, I'm left with never using it in an app.

I would appreciate any help you can provide.
 
V

victorcamp

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?
 

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