I am writing a JavaScript utility for myself as a learning experience, and my question is. When I need to throw an exception in my code when testing an argument or use something, should I use throw new TypeError();or throw TypeError();, or should I not check at all and just let it work with an error? I'm not sure if this is important. Of course, I understand the dynamics of exception handling, but is it better for me to deal with these exceptional cases as a lib writer or to let the user test them? For example, if I or someone using my library calls a method from the library with the wrong type of argument, which I should choose, if any.
var Lib = {
method1: function( /* bool */ do_task ) {
do_task = do_task || false;
if ( typeof do_task !== "boolean" ) {
throw new TypeError( "argument[0] should be true or false" );
} else {
if ( do_task === true ) {
} else {
}
}
}
};
- , , . . , . ? ?