Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw InvalidArgumentException JavaScript?

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:

  1. 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!
  2. 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.

  1. Should I create a new object like in the throw documentation, or create an Error Message?
  2. Doesn't JavaScript have a InvalidArgumentException object that I can use?
  3. How should I proceed in order to have a maintainable way to throw errors that uses ECMA6?
like image 997
Flame_Phoenix Avatar asked Jul 01 '16 12:07

Flame_Phoenix


People also ask

How do you throw a message 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. If no catch block exists among caller functions, the program will terminate.

How do you throw a typescript error?

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.


1 Answers

After doing research, I now have come down to a solution that I like. Special kudos to Toan, I would gladly chose his answer, but since I do feel that it is still a little bit incomplete, I decided to create my own answer with my own findings. Hope it helps someone !

Problems:

  1. Using the solution proposed by Toan: https://stackoverflow.com/a/38146237/1337392

  2. It is a possible, albeit if you want customization you do need to create your own object.

Answers:

  1. If you want to handle exceptions based on their types like in Java, you should create a new object like in the document. (by Toan Nguyen)
  2. Javascript does not have InvalidArgumentException (by Toan Nguyen)
  3. After reasearching, this solution is the one I found: https://stackoverflow.com/a/32750746/1337392

Credits:

  • Toan Nguyen, for the first answers and his great ideas
  • David Wickström, for suggesting an article that led to an article that led to a blog where the answer I was looking for was.

Thanks for all the help guys! kudos++ for all!

like image 51
Flame_Phoenix Avatar answered Sep 19 '22 07:09

Flame_Phoenix