10 / 12

What happens if a Base class constructor throws an exception? Does the Derived object get created?

Difficulty: 6/10
constructor exception handling, inheritance initialization, object lifecycle

Base Constructor Failure

If a base-class constructor throws an exception, construction of the derived object does not complete successfully. The derived constructor body is not reached because the base portion of the object must be initialized before the derived portion. The runtime propagates the exception according to the language's exception rules. From a design perspective, constructors should validate required state and establish invariants, but they should generally avoid complex external operations that can make object creation unpredictable.

javascript
  1. 1

    Base construction occurs before derived construction.

  2. 2

    If base construction fails, derived construction does not complete.

  3. 3

    The derived constructor body is not executed after a failed base construction.

  4. 4

    The object is not returned as a successfully constructed instance.

  5. 5

    Constructor failures should leave no usable partially initialized object exposed.

Scenario Questions

0-2 years experience

  1. 1You have a class Base whose constructor reads a config file and throws if the file is missing. Derived adds extra fields. What will happen if you write `new Derived()` and the config file isn’t present?
  2. 2If a Base constructor throws during the creation of a Derived object, does the Derived part of the object ever get allocated or its constructor run?

2-5 years experience

  1. 1While debugging a feature, you see a crash when creating a Derived instance, but the stack trace points to the Base constructor throwing. Explain why the Derived object wasn’t created and how you would modify the code to handle the failure gracefully.
  2. 2You need to log a failure when the Base constructor throws, but still continue processing other objects. How would you structure the code to catch the exception without leaking resources?

5-8 years experience

  1. 1Your service creates thousands of objects that inherit from a Base which opens a network socket in its constructor. Occasionally the socket open fails and throws, leading to partial objects and resource leaks. How would you redesign the initialization to avoid these problems at scale?
  2. 2Discuss the trade‑offs of moving the risky I/O operation out of the Base constructor into a factory method versus keeping it in the constructor, considering performance and error handling.

8+ years experience

  1. 1A legacy codebase has many derived classes that rely on Base constructors performing I/O and potentially throwing. The organization wants a unified error‑handling strategy that works across services while preserving backward compatibility. What architectural changes would you propose?
  2. 2Design a pattern or framework that ensures any exception during base initialization results in a well‑defined fallback object, without breaking existing callers across multiple teams.

Follow-up Questions

  • What happens to any members of the derived class that were already initialized before the exception?
  • How can you ensure resources allocated in the base are cleaned up when the exception propagates?
  • Is it possible to catch the exception inside the derived constructor, and what would that imply?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.