0

I'm slowly working my way to understanding the subtleties of JavaScript. At this time, I'm trying to instantiate an instance of the JavaScript Error object. I know that I can do the following:

var error = new Error('The value is invalid');
error.name = 'ArgumentException';
throw error;

However, I'm trying to figure out/understand if there's a way to condense this instantiation down to a single line. For instance, I'd like to do something like the following:

var error = new Error().{ message:'The value is invalid', name:'ArgumentException'};
throw error;

Is it possible to instantiate an instance of the JavaScript Error object while setting both the message and name properties?

Thank you!

6
  • The name property is inherited from Function, so you can't set it directly when instanciating an Error object as far as I know. Commented Jan 19, 2014 at 16:05
  • Why would condensing this to a single line make it better? Take a look at the documentation developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 19, 2014 at 16:05
  • 2
    For a one-liner, closest you'll get is something like throw (function() { var e = new Error("Whoops!"); e.name = 'ArgumentException'; return e; })(); Commented Jan 19, 2014 at 16:06
  • It's just a preference to get it to a single line. Plus, I'm trying to learn more about the JavaScript language. Commented Jan 19, 2014 at 16:07
  • jsfiddle.net/kt5TR/2 Commented Jan 19, 2014 at 16:07

3 Answers 3

1

You can assign error.name on the same line as throwing it.

If the right hand side of an assignment is a truthy value, which yours is (a non-empty string), the assignment statement will be truthy, meaning you can run on with &&. The last truthy statement will be sent back to throw at the beginning of the line and will be thrown.

var error = new Error('The value is invalid');
throw (error.name = 'ArgumentException') && error;

Without creating your own Error class with name set to ArgumentException, this is as few statements as you can get to without using an Immediately Invoked Function Expression (IIFE) to have one block statement.

EDIT: adeneo correctly used an IIFE which is one statement, even though it contains multiple statements within it as a block. I've edited my last paragraph to include this.

Sign up to request clarification or add additional context in comments.

Comments

1

If you really want everything on a single line, you can do something like

var error = Object.defineProperty(new Error('The value is invalid'), 'name', {value: 'ArgumentException'});

You could also throw it on the same line

throw Object.defineProperty(new Error('The value is invalid'), 'name', {value: 'ArgumentException'});

I'm not really fond of how it looks like, but I guess this answers your question.

Comments

0

I created a custom Error object a while ago following along Mozilla recommendations. Basically it creates an object in an OO fashion (pay attention to prototype and constructor). Caution there are some limitations (type-specific catches; read the resources I mentioned below):

// Create a new object, that prototypilly inherits from the Error constructor.
function MyError(message) {
  this.name = "MyError";
  this.message = message || "Default Message";
}
MyError.prototype = new Error(); 
MyError.prototype.constructor = MyError;

try {
  throw new MyError();
} catch (e) {
  console.log(e.name);     // "MyError"
  console.log(e.message);  // "Default Message"
}

try {
  throw new MyError("custom message");
} catch (e) {
  console.log(e.name);     // "MyError"
  console.log(e.message);  // "custom message"
}

For further information: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Error

The Mozilla page refers to the following question (and caveats you must be aware of): What's a good way to extend Error in JavaScript?

1 Comment

This is good for a larger code base. A linked answer that SO displayed in the sidebar adds calling the original class in the constructor, and IMO is worth reading: stackoverflow.com/a/1382129/3150057

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.