How to throw InvalidArgumentException JavaScript?
Asked Answered
F

3

19

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?
Fractional answered 1/7, 2016 at 12:53 Comment(1)
Maybe this is helpful? #13078181Sclar
F
17

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://mcmap.net/q/636593/-how-to-throw-invalidargumentexception-javascript

  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://mcmap.net/q/73705/-what-39-s-a-good-way-to-extend-error-in-javascript

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!

Fractional answered 6/7, 2016 at 14:9 Comment(0)
H
9

I have been trying to familiarize myself with JavaSctipt [sic] error handling, and I know I can throw exceptions [....] but tomorrow I may want BONG [....]

You could throw a TypeError or a SyntaxError if they make more sense. But no need to get too fancy. Simply throw an Error with a message as it looks as though you're already doing:

if (myArray && myArray.length < mySize) throw new Error('`mySize` must be larger');

And if you decide tomorrow you want BONG:

if (!myBong) throw new Error('Officer, that is not `myBong`');

For more info on built-in errors types, read the docs.

Hau answered 8/6, 2017 at 8:32 Comment(0)
P
8

Please see the answer below:

  1. If you want to handle exceptions based on their types like in Java, you should create a new object like in the document.

  2. If you don't want to look for the message everywhere, create a new error list like you do in Java. For instance:

    let errors = {
        invalidOperation: 'Invalid operation',
        unAuthorized: 'You are not authorized to use this function',       
    }
    

    and use them instead of hard-coded strings like

    throw new InvalidOperationException(errors.invalidOperation);
    
  3. JavaScript does not have InvalidArgumentException.

Paradise answered 1/7, 2016 at 13:6 Comment(4)
So, you are saying I have to create my own object, right?Fractional
Yes, that is the way to do if you have logic handling exceptions based on their types. If you just have a simple try/catch {}, and then work on the message then you can use the generic Error type.Paradise
is InvalidOperationException something that JavaScript has, or did you create it? I could not find any references to it in the Error doucmentation.Fractional
I created InvalidOperationException. Javascript does not have such an error.Paradise

© 2022 - 2024 — McMap. All rights reserved.