Subj : Re: Exceptions/Errors/Line Numbers To : netscape.public.mozilla.jseng From : Peter Wilson Date : Wed Jun 29 2005 12:05 am Brian Barnes wrote: > A quick problem. In my C code (which represents functions on an > object), if an argument is wrong, I throw an exception. Right now, I do > this: > > JS_SetPendingException(cx,script_string_to_value("my error text")); > > script_string_to_value is just a wrapper to turn the string into a JSVAL. > > This works fine. When reacting to the error (in C), I get the pending > exception, check if it's a string, then use the string. If it's not a > string, then JS itself throws and exception, and I can call: > > report=JS_ErrorFromException(cx,errval); > > And get the report. > > The problem: When I make an exception, I don't get the line numbers and > other information. I want to be able (in C) to construct a exception > like the engine itself does, so I can have the line numbers where it > occurred. Calling: > > JS_ReportError(cx,"my error text"); > > Doesn't work, as it's not an exception (maybe this is my problem, and I > don't understand this correctly.) So, what solutions do I have? > > [>] Brian In my code I use JS_ReportErrorNumber, which is in the jsapi.h header (so I believe legal to use!) but unfortunately there is no documentation. I looked into this long while back and worked out how to use it, then hid the complexity in my own function so can't remember the logic. The prototype is: JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback, void *userRef, const uintN errorNumber, ...); I pass the following paramters: userRef : NULL, not used errorNumber : JSMSG_READ_ONLY and I pass a pointer to the following function to 'errorCallback': static JSErrorFormatString XmlIf_Format = { "{0}",1 }; const JSErrorFormatString * XmlIf_ErrorFormatFn(void *userRef, const char *locale, const uintN errorNumber) { return &XmlIf_Format; } (No idea after all this time why this works - but it's used successfully to throw catchable exceptions in my own code that can be caught by JavaScript). I'm sure others in here will be able to explain to you why this works - by brain is just a little rusty today. Pete -- Peter Wilson : Architect : http://www.whitebeam.org .