views:

152

answers:

3

If I have a function like the following:

function catchUndefinedFunctionCall( name, arguments )
{
    alert( name + ' is not defined' );
}

and I do something silly like

foo( 'bar' );

when foo isn't defined, is there some way I can have my catch function called, with name being 'foo' and arguments being an array containing 'bar'?

A: 
someFunctionThatMayBeUndefinedIAmNotSure ? someFunctionThatMayBeUndefinedIAmNotSure() : throw new Error("Undefined function call");
M28
A: 
try {
 foo();
}
catch(e) {
   callUndefinedFunctionCatcher(e.arguments);
}

UPDATED

passing e.arguments to your function will give you what you tried to pass originally.

Levi Hackwith
`arguments` is not a property of the `Error` object, it is a property of functions. In your example `e` is an error object and so `e.arguments` will be undefined.
Andy E
I think it is just that kind of checking the catch-all strategy people want to get away from.
npup
I just ran the following in chrome:try { bar('foo');}catch(e){ alert(e.arguments); for(x in e) { alert(x) }}it alerts 'foo' and then alerts 'arguments' as a property of e. Am I crazy? Or just still wrong?
Levi Hackwith
+2  A: 

There is in Mozilla Javascript 1.5 anyway (it's nonstandard).

Check this out:

var myObj = {
    foo: function () {
        alert('foo!');
    }
    , __noSuchMethod__: function (id, args) {
        alert('Oh no! '+id+' is not here to take care of your parameter/s ('+args+')');
    } 
}
myObj.foo();
myObj.bar('baz', 'bork'); // => Oh no! bar is not here to take care of your parameter/s (baz,bork)

Pretty cool. Read more at https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/NoSuchMethod

npup
This is very cool and nearly exactly what I was looking for - is there an equivalent that works in IE 6+?
Emory