Windows Application error handling question

  • Thread starter Thread starter Mark_B
  • Start date Start date
M

Mark_B

I have a usb device that controls a power supply. That usb device must
perform a procedure, named Zero_outputs(), in the event of any error.
Clearly, within my main form (the only form in this application) I can have
each method envelope a Try-Catch statement that calls Zero_outputs() in the
catch clause.

However, I fear that certain application errors might occur outside of the
context of any given method in my main form. Is there a way to execute
Zero_outputs() whenever any error, of any kind, occurs in my application?
 
I have a usb device that controls a power supply. That usb device must
perform a procedure, named Zero_outputs(), in the event of any error.
Clearly, within my main form (the only form in this application) I can have
each method envelope a Try-Catch statement that calls Zero_outputs() in the
catch clause.

However, I fear that certain application errors might occur outside of the
context of any given method in my main form. Is there a way to execute
Zero_outputs() whenever any error, of any kind, occurs in my application?

Use Application.ThreadException to catch exceptions in all forms
threads.
Please read carefully: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx

NOTE (from the above link):
To use this event, you must attach a handler before you call
Application..::.Run.

If an exception occurs in the main application thread and no catch
block in the call stack handles it, the default exception handler
catches it and terminates the application. If an exception occurs in a
thread other than the main application thread, the thread exits, but
the application continues to run.

AND, ALSO IMPORTANT:
Because this is a static event, you must detach your event handlers
when your application is disposed, or memory leaks will result.

Hope it helps.
 
However, I fear that certain application errors might occur outside of the
context of any given method in my main form. Is there a way to execute
Zero_outputs() whenever any error, of any kind, occurs in my application?

Such as what?
 
That's a really good question actually. I can think of two right off hand:
out of memory and stack overflow. But I do not know about others. I sense
there may be several.

The target is "intrinsic safety". I am looking for an approach which will
trap all errors, even those I do not know about as well as any I cannot
anticipate or predict.

I read nano2k's helpful article. It certainly does a nice job of terminating
the program but - and this is the clincher - prior to aborting the program a
specific method (Zero_outputs) must execute to bring pins low on a device...
shutting down a power supply. Right now I have a try statement in program.cs
that looks like this:

try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
catch
{
in here is a replica of Zero_outputs();
}

This approach seems to work pretty well in the case where an unhandled
exception occurs in a method in the application's main form (again, the only
form for this application). I could write test code that that generates stack
overflow and even out of memory errors. But, your question will still haunt
me. “Such as what� I will wonder, is there an error type I haven't
anticipated?
 

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

Back
Top