From:  "Dan Korn" <dan@dankorn.com>
Date:  02 Jun 2006 01:01:43 Hong Kong Time
Newsgroup:  news.mozilla.org/netscape.public.mozilla.jseng
Subject:  

Why not make all errors catchable?

NNTP-Posting-Host:  65.114.117.130

I wanted to allow errors thrown from JS_ReportError to be catchable in
scripts.  So I merged the patch for bug 215173
(http://bugzilla.mozilla.org/show_bug.cgi?id=215173) to our slightly
modified copy of JS 1.5, which is embedded into our application.  Then
I applied the fix for related case 319384
(http://bugzilla.mozilla.org/show_bug.cgi?id=319384).

Then, in order to allow errors raised with JS_ReportError to be
catchable, as well as those raised by JS_ReportErrorNumber, I added the
following line to js_ReportErrorVA (in jscntxt.c), right before the
call to ReportError:

    report.errorNumber = JSMSG_UNCAUGHT_EXCEPTION;

And I also had to change the EXCEPTION_NAME for
JSMSG_UNCAUGHT_EXCEPTION in js.msg from JSEXN_NONE to JSEXN_ERR.  With
these changes, I'm able to catch errors raised by JS_ReportError as
exceptions.

Then I tried catching some other errors, like JSMSG_MORE_ARGS_NEEDED
("{0} requires more than {1} argument{2}"), as raised from
JS_ConvertArguments.  No dice, unless I changed the type for that one
from JSEXN_NONE to JSEXN_ERR as well.

This got me thinking, why shouldn't all interpret-time errors be
catchable?  So instead of changing all the types in js.msg, I changed
this code in js_ErrorToException (in jsexn.c):

     if (exn == JSEXN_NONE)
-        return JS_FALSE;
+        exn = JSEXN_ERR;

And this works!  I don't need to modify js.msg at all.  I can catch any
interpret-time errors raised from JS_CallFunction, including
JSMSG_MORE_ARGS_NEEDED, JSMSG_CANT_CONVERT, and others.  (Of course, I
still can't catch compile-time errors raised during JS_CompileFunction,
such as syntax errors, but I don't expect to be able to do that.)

So my question is, why shouldn't I be able to catch all of these errors
by default?  Why not make all non-syntax errors into catchable
exceptions?

Thanks,
Dan

P.S.  Being able to search the netscape.public.mozilla.reviewers in
Google was very helpful to me in figuring this out.  I know that list
is deprecated, but can the new mozilla.dev.super-review list be somehow
archived as a Google Group as well?