JavaScript infrastructure such that propagated errors don't get lost
<p>My group is starting a new project.  We are thinking about organizing the JavaScript in such a way so that any JavaScript errors don't get lost but rather get caught and sent to the server to be logged.  For namespacing I want to keep it simple so I'm using something like this:</p>

<pre><code>var my_namespace = function() {
    function myFunction(input) {
        if (input &lt; 0) {
            throw "input must be positive";
        }

        return 'result';
    }

    return {myFunction: myFunction};
} ();
</code></pre>

<p>So now I can invoke my_namespace.myFunction(-22) but when the error will be thrown it will get lost.  There will be many namespaces each one in it's own .js file (maybe somebody has a better idea about namespace schema).</p>

<p>So, my question is, how to like "surround" namespaces so that errors will not get lost?</p>

<hr>

<p>Actually @Relic gave a good idea.  I'm going to write below the code that will create the namespace "my_namespace" and surround the initialization by jQuery with try-catch:</p>

<pre><code>var my_namespace = function() {
    function init() {
        throw "an exception during initialization";
    }

    return {init: init};
} ();

$(document).ready(function() {
    try {
        my_namespace.init();
    } catch (e) {
        // handle error
    }
});
</code></pre>

<p>I'm going to experiment with what happens after it does initialization, that is, with the event handling.</p>

<hr>

<p>Yep, just as I thought, event handling exceptions will not be caught.  I'll research some more and return.</p>
