Subj : Re: How do I raise an exception? To : netscape.public.mozilla.jseng From : Peter Paulus Date : Tue Aug 02 2005 04:33 pm 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. > > I've found some prior posts that seems to indicate that JS_ReportError > should do this, but doesn't because of a bug. > > Regards, > > Thomas Hello Thomas, The documentation at http://www.mozilla.org/js/spidermonkey/apidoc/ doesn't say, but there are some public API calls for exceptions: Look at jsapi.h extern JS_PUBLIC_API(void) JS_SetPendingException(JSContext *cx, jsval v); I believe jsval can be any type of value: scalar or object. In order to throw an object you create it first with JS_NewObject(): obj = JS_NewObject(cx, &ExceptionClass, NULL, NULL); v = OBJECT_TO_JSVAL(obj); Scalars could be created with JS_NewUCString() and JS_NewDouble(). Boolean and Integer can do not have to be created but can be converted with BOOLEAN_TO_JSVAL() and INT_TO_JSVAL. ExceptionClass is defined in jsexn.c and defines the javascript 'Error' class. Of course you can throw any class by using the appropriate JSClass* parameter here. You could try and subclass the javascript 'Error' object. Note bene: To be honest, I haven't tried this (it is still on my todo list). With kind regards, Peter Paulus .