# Failing constructor intentionally to stop the instantiation of an object

**URL:** <https://discourse.processing.org/t/failing-constructor-intentionally-to-stop-the-instantiation-of-an-object/45719>\
**Category:** Beginners\
**Created:** [February 7, 2025, 4:51am UTC](https://discourse.processing.org/t/failing-constructor-intentionally-to-stop-the-instantiation-of-an-object/45719 "2025-02-07T04:51:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 7, 2025, 4:51am UTC](https://discourse.processing.org/t/failing-constructor-intentionally-to-stop-the-instantiation-of-an-object/45719/1 "2025-02-07T04:51:57Z")

</div>

I had a function **outside** a class that performs some checks before making the object from the class. If the incoming parameter was faulty, it would return **null** instead of the object.

Then it occured to me that it would be nicer to have this function as a 2nd constructor **inside** the class (also returning null when failed). But I thought to return null we can leave the constructor early, thus stopping the object from being made. This doesn’t work, so is this even possible?

Here is my new constructor:

```auto

  // ------------------------------

  Formula (String in_) {
    // parseStringToFormula

    Formula myFormula=null;

    fullText=in_;

    if (in_.charAt(0)!='=') {
      // this = null;
      return; // !!!!!!!!!!!!
    }
   
    ........
    ......

```

Thanks to all!

Warmest regards and a great 2025,

Chrisir

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 7, 2025, 4:57am UTC](https://discourse.processing.org/t/failing-constructor-intentionally-to-stop-the-instantiation-of-an-object/45719/2 "2025-02-07T04:57:17Z")

</div>

You might try out to `throw` an Exception in order to interrupt the `class` instantiation.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 7, 2025, 5:08am UTC](https://discourse.processing.org/t/failing-constructor-intentionally-to-stop-the-instantiation-of-an-object/45719/3 "2025-02-07T05:08:40Z")

</div>

yeah, but there is no real error. Can I just say throw?  
can you post an example?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 7, 2025, 1:18pm UTC](https://discourse.processing.org/t/failing-constructor-intentionally-to-stop-the-instantiation-of-an-object/45719/4 "2025-02-07T13:18:57Z")

</div>

This is an example that `throws` an unchecked exception, which doesn’t require a try/catch block:

```auto
class Formula {
  String fullText;

  Formula(final String input) {
    if (input.charAt(0) != '=') throw new IllegalArgumentException(
      input + " must begin with the '=' character!"
    );

    fullText = input;
  }
}

```

Same as above, but it `throws` a checked exception which requires a try/catch block:

```auto
class Formula {
  String fullText;

  Formula(final String input) throws java.text.ParseException {
    if (input.charAt(0) != '=') throw new java.text.ParseException(
      input + " must begin with the '=' character!", 0
    );

    fullText = input;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 9, 2025, 9:29am UTC](https://discourse.processing.org/t/failing-constructor-intentionally-to-stop-the-instantiation-of-an-object/45719/5 "2025-02-09T09:29:48Z")

</div>

Thanks a lot! Gonna test it.
