About 475,000 results
Open links in new tab
  1. When is it right for a constructor to throw an exception?

    One-stage constructors should throw if they fail to fully initialize the object. If the object cannot be initialized, it must not be allowed to exist, so the constructor must throw.

  2. constructor - JavaScript | MDN - MDN Web Docs

    Mar 13, 2025 · While the base class may return anything from its constructor, the derived class must return an object or undefined, or a TypeError will be thrown. If the parent class …

  3. JavaScript Class constructor Method - W3Schools

    The constructor() method is called automatically when a class is initiated, and it has to have the exact name "constructor", in fact, if you do not have a constructor method, JavaScript will add …

  4. What should a JavaScript constructor return if it fails?

    Mar 1, 2011 · Returning any non-object from the constructor is practically the same as exiting the constructor. (The constructor will return a new object, with a prototype if one was specified.) …

  5. throw - JavaScript | MDN - MDN Web Docs

    Mar 13, 2025 · The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to …

  6. Custom errors, extending Error - The Modern JavaScript Tutorial

    JavaScript allows to use throw with any argument, so technically our custom error classes don’t need to inherit from Error. But if we inherit, then it becomes possible to use obj instanceof …

  7. Is it considered "good practice" to throw an exception in a constructor

    Is it considered "good practice" to throw an exception in a constructor ? Yes, because not all constructors are meant to be used directly. Take for example, the HTMLElement() constructor. …

  8. JavaScript Object Constructors - W3Schools

    JavaScript has built-in constructors for all native objects: The Math() object is not in the list. Math is a global object. The new keyword cannot be used on Math. Did You Know? Use object …

  9. JavaScript Error() constructor - GeeksforGeeks

    Jul 7, 2022 · Javascript Error() constructor is used to create a new error object. Error objects are arising at runtime errors. The error object also uses as the base object for the exceptions …

  10. Should I throw an exception in constructors - Stack Overflow

    Jun 15, 2019 · It is entirely acceptable to throw within the constructor. And usually the best practice is to fail fast and fail early. In your case for example, it is best to throw in the …