2

Mozilla says that we can define an object and throw it.

This way, we can encapsulate more than a simple string message and send it to the exception consumer (the catch block, or the then method of promises).

However, I don't want to constantly define types in my JS code.

Is it possible to throw anonymous objects?

I tried throw new Error({ firstKey: firstValue, secondKey: secondValue }) and it does not work. I get [object Object].

Big boy
  • 1,113
  • 2
  • 8
  • 23
  • That works fine, it just means that somewhere in your code, you're turning it into a string instead of looking at the object properties. – CertainPerformance Nov 24 '22 at 15:28
  • 1
    Your object needs a `toString` function for your example to work. Like `throw new Error({firstKey: 1, secondKey: 2, toString: () => 123})`. – Lain Nov 24 '22 at 15:30
  • try `throw { firstKey: firstValue, secondKey: secondValue }` directly – R4ncid Nov 24 '22 at 15:30
  • @R4ncid An Error object is usually a bit nicer to work with: https://eslint.org/docs/latest/rules/no-throw-literal – CertainPerformance Nov 24 '22 at 15:31
  • 1
    @CertainPerformance, simply open a browser window, open the dev tools, then write `throw new Error({name: 'somebody, age: 40})`. You see `[object Object]` and nowhere you have turned it into string. – Big boy Nov 27 '22 at 11:54
  • @Lain, I added that `toString` method and now I recieve the return value of that method. That's not what I want. I want to get the object. – Big boy Nov 27 '22 at 12:31
  • You can derive from Error to make the properties you need – Daniel A. White Nov 27 '22 at 15:31

3 Answers3

2

When an Error is created, the first parameter passed (if any) is converted to a string, and not used anywhere else. So, just by using the first parameter, there's no way to pass an object along (without using JSON.stringify, which would be ugly and also lose information).

But Errors can accept a second argument - an object with a cause property, which will then get put on the error object. You can use this to communicate any information you want, including objects.

const objForCause = {};
try {
  throw new Error(
    'Something went wrong',
    { cause: { prop: 'val', obj: objForCause } }
  );
} catch(err) {
  // Not all errors have causes
  // Ensure that it does before examining further
  if (err.cause) {
    console.log(err.cause);
    console.log(err.cause.obj === objForCause);
  }
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

In javascript, you can throw literally anything:

try {
  throw "a string";
} catch (error) {
  console.log("caught", error)
}

try {
  throw 10;
} catch (error) {
  console.log("caught", error)
}

try {
  throw {a: 1, b: 2};
} catch (error) {
  console.log("caught", error)
}

So to answer the question Is it possible to throw anonymous objects?: yes it's possible to throw an anonymous object.

Whether you should do so is another question.

Marco
  • 7,007
  • 2
  • 19
  • 49
0

If you read your own question statement carefully. This is what is being said.

  1. Define your own object
  const myobj={a:1,b:2};
  1. Then throw it.
  throw myobj;
  1. Use the object however you want
console.log(err);

Below is a working example.

try {
  const myobj = {
    a: 1,
    b: 2
  };
  throw myobj;
} catch (err) {
  console.log(err);
}

You can do all sorts of things depending upon your imagination I guess. even attaching a error object instance if you want.

innocent
  • 864
  • 6
  • 17