Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throw Error('msg') vs throw new Error('msg')

var err1 = Error('message'); var err2 = new Error('message'); 

What's the difference? Looking at them in the chrome console, they look identical. Same properties on the object and the same __proto__ chain. Almost seems like Error acts like a factory.

Which one is correct and why?

like image 504
Ilia Choly Avatar asked Nov 08 '12 17:11

Ilia Choly


People also ask

Does throw new error stop execution?

throw new Error('something went wrong') — will create an instance of an Error in JavaScript and stop the execution of your script, unless you do something with the Error.

What is throw error in JavaScript?

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 the first catch block in the call stack.

How do you throw an error in typescript?

To declare a function that throws an error, set its return type to never . The never type is used for functions that never return a value, in other words functions that throw an exception or terminate execution of the program.

What is the difference between throw error () and throw new error ()?

First of all, throw Error () and throw new Error () are functionally equivalent. Error object: An object containing the error information is generated and passed to catch as an argument. Error object has three properties: name, message, and stack. Error () constructor is used to create a new error object.

What is the difference between ‘MSG’ and ‘new error’?

As you can see in the images of both of the consoles, throw an error (‘msg’) and throw a new error (‘msg’) give the same results. Note: Error () and new Error () returns an object as a result. Error (…) is less reliable and consistent. new Error (…) is more reliable and consistent.

Can I throw an error that is not an instanceof error?

Throwing something that isn't instanceof Error isn't a great idea and complicates the rest of the code because now you can't rely on the normal error properties being available, also you can't trace your error back to its source because it has no stack. While the second example is okay, it's better to use custom errors that inherit from Error.

How to get the error value from the message property?

In Summary: 1 throw "My error": this creates an Error object and returns the primitive data extracted from the constructor "this"... 2 throw new Error("My error"): this returns you an object where you can access the error value from the message property. More ...


2 Answers

Both are fine; this is explicitly stated in the specification:

... Thus the function call Error(…) is equivalent to the object creation expression new Error(…) with the same arguments.

like image 93
pimvdb Avatar answered Oct 15 '22 16:10

pimvdb


Error does act like a factory, like some other native constructors: Array, Object, etc. all check something like if (!(this instanceof Array)) { return new Array(arguments); }. (But note that String(x) and new String(x) are very different, and likewise for Number and Boolean.)

That said, in case of an error, it's not even required to throw an Error object... throw 'Bad things happened'; will work, too
You can even throw an object literal for debugging:

throw {message:"You've been a naughty boy",        context: this,        args: arguments,        more:'More custom info here'}; 
like image 28
Elias Van Ootegem Avatar answered Oct 15 '22 16:10

Elias Van Ootegem