user58777
user58777

Reputation:

Any way to stop Javascript from failing silently?

One thing that is driving me nuts is how Javascript fails silently in many different situations.

(removed example because it confuses the point of my question)

Many times I have come across an error that will give an error message when typed into the Firebug console, but when it runs within the page script, it fails silently, even with the Firebug console active and open!

Some of these problems can be caught by Crockford's JsLint, but still many won't.

Isn't there a way to enable more error messages in the browser?

Can you do this at all without using javascript debugger environments? I find debuggers don't help me much. I usually sprinkle a few console.log() statements and can locate the problem in a minute. What drive me nuts is that silent errors in Javascript can go unnoticed for a long time, or show up in ways that are not obvious at all. It's even more frustrating because testing the statement in the console DOES give an error, so what's going on?

I have had the same problem with exceptions by the way, did anyone notice this? Often times my throw new statements don't work at all. But if I type the same thing in the console, it does.

Thanks for your helpful comments (first answers), but that is not my question. These tests are useful when you need to sanitize parameters to a class for example, when you don't know for sure about the environment. You don't want to test for existence of properties or classes where you expect them to be there; that would be bloating the code for no reason.

Upvotes: 21

Views: 7968

Answers (4)

matte
matte

Reputation: 1246

The code below will catch all errors in catch block:

var a;
try {
    a = new Foo.Apple();
} catch (err) {
    // Error handling
}

For more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

Upvotes: 6

Ken Franqueiro
Ken Franqueiro

Reputation: 10559

I think some of the answers to this question are misinterpreting the question. IIUC, the question is saying that the error is being squelched when this code is run inside a real webpage, but the OP doesn't want it to be. (Presumably for debugging purposes, mainly.)

My question would then be, where is this code really running in the context of the website? Chances are something else is squelching the error before it gets to you. For instance, some libraries might instinctively squelch errors that occur within specific callbacks (e.g. for XHR). However, if they're good, they also tend to provide a hook point for receiving notification if an error occurred.

Upvotes: 7

Prasanna Narayanan
Prasanna Narayanan

Reputation: 439

Agree with @sime Vidas, if the exception has to be handled and not left silent, first use the boolean condition and check if the value exists.

if(Foo.Apple){
\\your code
}
else
{
//Foo.Apple does not exist, do some exception handling here
}

Upvotes: 3

Šime Vidas
Šime Vidas

Reputation: 185893

If you are working with objects for which you are not sure if they exist at runtime or not, you have to check for their existence:

if (Foo && Foo.Apple) {
    // exists, do something with it
} else {
    // doesn't exist, do Plan B
}

Note that the expression (Foo && Foo.Apple) will first check if Foo exists, and only if it does, it will check if it has a property named Apple. If it has, the if-branch will execute.

If Foo does not exist, or if it does not contain the Apple property, the else-branch will execute.

Upvotes: 4

Related Questions