Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:38:38+00:00 2026-06-17T23:38:38+00:00

I have a static class that wraps around .NET events like UnhandledException and ProcessExit

  • 0

I have a static class that wraps around .NET events like UnhandledException and ProcessExit (of the CurrentAppDomain object), so that when the caller calls Initialize on the class, it subscribes my own handlers for these events, and later on the caller may add his own handler methods, using a dedicated API.
(It it designed that way so I am able to control the order of handlers execution once the event is fired).

My question is: Is it possible to unit test such a class ?

For instance, I would like to know that when an unhandled exception is thrown, my private handler has executed. the problem is that when I throw unhandled exception, that will be the “result” of the test (correct me if I’m wrong).
Same scenario will happen if I exit the process.

I was thinking that a possible solution might involve loading a new app domain, though I don’t know if that’s possible and how to technically approach that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:38:40+00:00Added an answer on June 17, 2026 at 11:38 pm

    To test UnhandledException you can throw the exception in a new thread:

    [TestMethod]
    public void UnhandledTest()
    {
        using (var okEvent = new ManualResetEventSlim())
        using (var notOkEvent = new ManualResetEventSlim())
        {
            UnhandledExceptionEventHandler handler = (o, e) =>
                {
                    if (e.ExceptionObject is ApplicationException)
                        okEvent.Set();
                    else notOkEvent.Set();
                };
    
            AppDomain.CurrentDomain.UnhandledException += handler;
    
            var thread = new Thread(() => { throw new ApplicationException(); });
            thread.Start();
    
            var result = EventWaitHandle.WaitAny(new[] { notOkEvent.WaitHandle, okEvent.WaitHandle }, 10000);
            Assert.AreEqual(1, result);
    
            AppDomain.CurrentDomain.UnhandledException -= handler;
        }
    }
    

    This doesn’t result in a failed test, because the test framework catches exceptions only on the thread that executes the test method.

    ProcessExit on the other hand will be raised when the current process is exiting, so it’s quite likely that the test framework will have shut down by then. So you have to run the actual test code in a different process. From the unit test method, you will start a new process, it will run the meat of the test and communicate back to the unit test method in the original process the result of the test.

    The new process is simply a separate .exe project. What it does is attach to ProcessExit, stop itself, and in the ProcessExit handler check that everything is working in order, then it communicates the result back to the process running the unit test method.

    You could do the communication from the unit test project to the .exe project using the command line, and do the communication back using some sort of IPC, like temporary files or named events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static class that looks like this: namespace Argus { static class
Suppose I have a static method of my class that returns an object of
I have a static class that I use as my Data Utils for my
So I have a static class that is supposed to be used as a
I have a static util class that does some string manipulation on a bit
I have a static methods class, Utils, that is basically for utility methods, its
I have class A such that: class A { static int i; A(); f1();
I currently have a helper class that I am using to obfuscate a static
Let's say I have a class (not a static class), A , that in
I'm working with Progress 10.1c I have a class that contains both static and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.