Subj : Re: How do I raise an exception? To : Thomas Sondergaard From : Brendan Eich Date : Wed Aug 03 2005 01:03 am Thomas Sondergaard wrote: > It seems that returning JS_FALSE from a JSNative c-function just results > in an error. How do I raise an exception? Preferably of type Error. Just returning false without doing anything else silently terminates the running script. That's not desirable generally ;-). > I've found some prior posts that seems to indicate that JS_ReportError > should do this, but doesn't because of a bug. No, you can use JS_ReportError to throw errors as exceptions, but they won't be localizable. The bug you may be referring to involves the hardwired error numbers enumerated with other data by js.msg, and how an embedding can't use JS_ReportErrorNumber or related APIs with its own catalog of messages. You can construct new exception objects and set them as pending exceptions (any value may be thrown, so you could throw a string or a number, or a boolean too [or null or undefined -- there, that's the list of types]), using JS_SetPendingException. If you want to make an instance of Error and throw it, you'll have to get the value of the "Error" property of a properly initialized or otherwise (lazy standard class resolution) configured global object, and save it with a rooted pointer or jsval. Then you can call this Error constructor function object reference with the JS_CallFunctionValue API, to create the exception object you wish to throw, passing appropriate arguments. If that all works, set the new exception object reference as the pending exception and return false. /be .