Windows Application error handling question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWFya19C?=

    Windows Application error handling question

    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?
  • nano2k

    #2
    Re: Windows Application error handling question

    On 15 Oct, 21:03, Mark_B <Ma...@discussi ons.microsoft.c omwrote:
    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.Thr eadException to catch exceptions in all forms
    threads.
    Please read carefully: http://msdn.microsoft.com/en-us/libr...exception.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.

    Comment

    • Peter Morris

      #3
      Re: Windows Application error handling question

      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?

      Comment

      • =?Utf-8?B?TWFya19C?=

        #4
        Re: Windows Application error handling question

        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.Ena bleVisualStyles ();
        Application.Set CompatibleTextR enderingDefault (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?

        Comment

        Working...