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 6200159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:19:01+00:00 2026-05-24T04:19:01+00:00

In the constructor of an object, Listener , we take an argument and subscribe

  • 0

In the constructor of an object, Listener, we take an argument and subscribe to one of its events. If an exception is thrown within the constructor after the event is subscribed the OnSomethingChanged() method is still called when the event is raised – even through the object was not successfully constructed and, as far as I’m aware, no instance exists.

Now I can fix this by obviously re-factoring the design slightly, however I’m more interested in why an instance method is called even though the constructor did not complete successfully? If the method uses any local variables that have not been initialised before the exception then obviously it goes BOOM!

class Program
{
    static void Main(string[] args)
    {
        Input input = new Input();

        try
        {
            new Listener(input);
        }
        catch (InvalidOperationException)
        {
            // swallow
        }

        input.ChangeSomething(); // prints "Something changed!"
    }
}

public class Listener
{
    public Listener(Input input)
    {
        input.SomethingChanged += OnSomethingChanged; // subscibe

        throw new InvalidOperationException(); // do not let constructor succeed
    }

    void OnSomethingChanged(object sender, EventArgs e)
    {
        Console.WriteLine("Something changed!");
    }
}

public class Input
{
    public event EventHandler SomethingChanged;

    public void ChangeSomething()
    {
        SomethingChanged(this, EventArgs.Empty);
    }
}
  • 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-05-24T04:19:02+00:00Added an answer on May 24, 2026 at 4:19 am

    While throwing an exception from a constructor means an instance may potentially end up in an incomplete state, doing so doesn’t stop the instance itself from being created and stored in memory (as that happens before its constructor is called).

    Furthermore, the event handler has already been bound by the time you throw the exception, so raising the event will cause the handler to be invoked.

    To quickly illustrate the first point, if you gave Listener a field to initialize in its constructor, then tried to initialize it after throwing the exception (which obviously isn’t going to work):

        string foo;
    
        public Listener(Input input, string f)
        {
            input.SomethingChanged += OnSomethingChanged;
    
            // Because this is thrown...
            throw new InvalidOperationException();
    
            // ... this never happens
            foo = f;
        }
    

    And then tried to access it in its OnSomethingChanged handler:

        void OnSomethingChanged(object sender, EventArgs e)
        {
            Console.WriteLine("Listener.foo = " + foo);
        }
    

    Regardless of how you call new Listener(...), the output would be

    Listener.foo = 
    

    simply because the listener didn’t get a chance to initialize its foo field. Although it wasn’t fully initialized, it’s still a complete object in terms of allocation.

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

Sidebar

Related Questions

I have a mocked object that is passed as a constructor argument to another
One can create an anonymous object that is initialized through constructor parameters, such as
as i currently understand, if an event listener is added to an object with
With WatiN 1.3, the IE object constructor throws an exception on my machine. See
I heard that a private constructor prevents object creation from the outside world. When
Doesn't object initialization outside of a constructor break encapsulation ? Given: class MyClass {
For the ExcelPackage constructor you need a FileInfo object. I rather use some kind
How is object of serialized class created dynamically without calling the constructor when de-serialization
I have a Uri object being passed to a constructor of my class. I
When you instantiate a new object by calling constructor, i.e. Foo bar = new

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.