Objective
Throw InvalidArgumentException in a JavaScript method like one does in Java or similar languages.
Background
I have been trying to familiarize myself with JavaSctipt error handling, and I know I can throw exceptions using the throw
keyword.
To achieve this, I read the throw documentation on MDN and the Error documentation on MDN as well.
Problem
So far this is my code:
if (mySize >= myArray.length)
throw new Error("InvalidArgumentExcpetion - BANG!!!!");
This code has some problems for me:
- I have the text in the exception itself. Right now I have BANG, but tomorrow I may want BONG and if I decide to change it, I have to look everywhere!
- I am using an Error, and not really a new object with the type InvalidArgumentExcpetion. Is this the correct way?
Questions
So now I am confused.
- Should I create a new object like in the throw documentation, or create an Error Message?
- Doesn't JavaScript have a InvalidArgumentException object that I can use?
- How should I proceed in order to have a maintainable way to throw errors that uses ECMA6?