In javascript, I have noticed two ways of throwing an error:
1)
throw "An error";
2)
throw new Error('An object error')
Is there any advantage of choosing one way over the other?
Is one considered a better practice?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An
Errorobject has nice little extras likeerror.nameand (in Firefox)error.stack.If you need those, do throw an error explicitly. However, most people merely use the
error.toString()method (often called implicitly). In the latter case it’s overkill to create an Error object in the first place, so throwing a string works just as well.