FAQ: Learn JavaScript: Error Handling - Constructing an Error

This community-built FAQ covers the “Constructing an Error” exercise from the lesson “Learn JavaScript: Error Handling”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise Constructing an Error

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

After running the code with

console.log(Error('User missing name'));

I got the following output in the console:

Error: User missing name
    at Object.<anonymous> (/home/ccuser/workspace/error-handling-error-construction/main.js:2:13)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3
Will logging the error stop our program from running?

What are these messages beginning with at?

The trace begins at the point of exception, only the error is arbitrarily thrown, so that is where the trace starts. The source location is given in the second line of the error message. All the rest can be ignored. They are just failing because something before has failed.

2 Likes

Thank you for a very quick response! Good to know that I can ignore these error messages except the first one on where the error occurs in the script.

1 Like

https://www.codecademy.com/courses/javascript-errors-debugging/lessons/error-handling/exercises/runtime-errors

Hi there,
this exercise (url above) introduces the JavaScript Error function: “The Error function takes an argument of a string which becomes the value of the error’s message property.”

It seems pretty useful for custom errors so I tried to know more about its use by reading more material about it.
Unfortunately I’ve been unsuccessful so far in my attempts to finding anything on Google about JavaScript Error function. All links I’ve got send me to the Error Object and plenty of ways to handle errors with the try…catch statement and “new Error” syntax but nothing about the Error function.
I’ve read anything I found in MDN web docs about Javascript errors and the Error function was nowhere to be found. At least by me.
In this exercise’s example syntax
“console.log(Error(‘Your password is too weak.’));”
Error doesn’t seem to behave like an Object but rather like a Function being passed a String as unique argument. It fits the above definition but I can’t find it outside Codecademy course so far.

Meanwhile this other example
“console.log(new Error(‘Your password is too weak.’));”
seems to fit what I understand so far about Error. A new instance of the Error class, i.e. an Object, is created and its message property’s value is passed a String.

The lesson says that both ways will lead to the same functionality with no further explanation. It is where I get confused.
What am I getting wrong here?

Error is a class, and new is an instantiation operator that will give us an object from that class. However, we can anonymously invoke a transient object of the class using its own signature. That is because it has a constructor which is the working function when we invoke the class.


Cancel that.

Picture,

class Oops {
    constructor (message) {
        this.message = message;
    }
}
 > console.log(Oops('Oops!'))
<- Uncaught TypeError: Class constructor Error cannot be invoked without 'new' at <anonymous>:1:13

So Error must be a special class that permits transient invocation. Our class definition must be missing something. Needless,

 > Error('Oops!')
<- Error: Oops!
    at <anonymous>:1:1
 > 

Thanks for the quick answer. I follow your logics and I reach the same conclusion. I can live with the special class thing.
Still it’s a bit tricky. I definitely need to find out more about this one.

1 Like

Shouldn’t fail someone for using ` over or ". Is there a reason to discourage this?

It looks to be failing for a completely different reason… ‘no’ vs. ‘not’.

2 Likes