Throwing Strings Instead of Errors

Throwing strings instead of Errors

While it is okay possible to throw any value, it is generally considered poor form to throw anything other than an instance of Error or one of its subclasses. There are several reasons for this:

  1. Catching code may expect the thrown object to have the usual message, stacktrace, and name properties that appear on Errors.
  2. Lack of a stacktrace makes debugging problematic, especially in the case of uncaught exceptions / unhandled rejections. E.g. Debugging an "Uncaught [Object object]" error can be particularly painful.

What is the difference between `throw 'foo'`, `throw Error('foo')`, `throw new Error('foo')`?

throw is an expression which halts the function and generates an exception. Whatever directly follows throw is passed along in the exception. Think of it as a function with syntax sugar, so instead of writing throw('message') you write throw 'message'. throw new Error('message') is just like throw 'message' except an object is being passed along instead of a string literal.

There is no difference between throw Error('message') and throw new Error('message'): many of the core JavaScript objects allow for the creation of a new object without the new constructor and Error happens to be one of them.

That being said, you should always use throw new Error('message'). The Error object contains a stacktrace and other useful debugging information which is lost when you use a string literal. Creating objects using ES6 classes requires the use of new and extending Error via a class is the only way to preserve stacktraces. Creating a custom error class makes error handling much more uniform.

See Also: extremely elaborate illustration.

C++ Exception - Throw a String

You are currently throwing a const char* and not a std::string, instead you should be throwing string("error")

edit: the error is resolved with

throw string("exception ! error");

Why is the 'in' operator throwing an error with a string literal instead of logging false?

In a sense it is a matter of timing. String literals do not have any properties. The reason that you can call methods and lookup properties on primitive strings is because JavaScript automatically wraps the string primitive in a String object when a method call or property lookup is attempted. JavaScript does not interpret the in operator as a method call or property lookup so it does not wrap the primitive in an object and you get an error (because a string primitive is not an object).

See Distinction between string primitives and String objects

Also, the same docs referenced in your question specifically note that using in on a string primitive will throw an error.

You must specify an object on the right side of the in operator. For
example, you can specify a string created with the String constructor,
but you cannot specify a string literal.

What is the difference between `throw new Error` and `throw someObject`?

The difference between 'throw new Error' and 'throw someObject' in javascript is that throw new Error wraps the error passed to it in the following format −

{ name: 'Error', message: 'String you pass in the constructor'
}

The throw someObject will throw the object as is and will not allow any further code execution from the try block, ie same as throw new Error.

Here is a good explanation about The Error object and throwing your own errors

The Error Object

Just what we can extract from it in an event of an error? The Error object in all browsers support the following two properties:

  • name: The name of the error, or more specifically, the name of the constructor function the error belongs to.

  • message: A description of the error, with this description varying depending on the browser.

Six possible values can be returned by the name property, which as mentioned correspond to the names of the error's constructors. They are:

Error Name          Description

EvalError An error in the eval() function has occurred.

RangeError Out of range number value has occurred.

ReferenceError An illegal reference has occurred.

SyntaxError A syntax error within code inside the eval() function has occurred.
All other syntax errors are not caught by try/catch/finally, and will
trigger the default browser error message associated with the error.
To catch actual syntax errors, you may use the onerror event.

TypeError An error in the expected variable type has occurred.

URIError An error when encoding or decoding the URI has occurred
(ie: when calling encodeURI()).

Throwing your own errors (exceptions)

Instead of waiting for one of the 6 types of errors to occur before control is automatically transferred from the try block to the catch block, you can also explicitly throw your own exceptions to force that to happen on demand. This is great for creating your own definitions of what an error is and when control should be transferred to catch.

throw Error('msg') vs throw new Error('msg')

Both are fine; this is explicitly stated in the specification:

... Thus the function call Error(…) is equivalent to the object creation expression new Error(…) with the same arguments.



Related Topics



Leave a reply



Submit