开发者

Do not stop JavaScript when it throws an exception

I am writing a Windows Sidebar Gadget using JavaScript. Now I would like to have all JavaScript exceptions caught and logged to a text file. One problem is that when an exception is thrown at any line, the next line will not be executed. Is it possible to catch exceptions automatically so that the following line of JavaScript can be executed.

Here is the sample code which demonstrates my problem:

try
{
    alert(document.getElementById("non-existing-element").value);
}
catch(ex)
{
}
alert("This is shown.");
alert(document.getElementById("non-existing-element").value);
alert("This is not shown.");

So one big try-catch-method whould allow to log the error but the rest of the code would not be executed. One solution would be to catch errors for ev开发者_高级运维ery line. But would be too much code for several thousand lines. (or I write a macro which does this for me) Maybe anyone can help me.

Thank you.


Instead of trying to catch errors, you should be trying to build proper logic to control them.

var myEle = document.getElementById("non-existing-element")

if (myEle != null)
   alert(dmyEle.value);

But you are describing suppressing errors

You can suppress error in JavaScript with a onerror event handler.

function handleErr(msg, url, line_no){ 
   var errorMsg = "Error: " + msg + "\n"; 
       errorMsg += "URL: " + url + "\n"; 
       errorMsg += "Line: " + line_no + "\n\n"; 

    alert(errorMsg); 

 return true;
} 

// Set the global onerror; 
onerror = handleErr;

More information here


No. Exceptions always propagate up to the closest containing try block and then out of the try block if it has no catch and the finally exits normally.

There are no resumable exceptions in JavaScript, so no way to declare a single global exception handler and resume all exceptions.


No easy solution but if you use something like jQuery and you do

$("#non-existing-element").attr('value') 

would be better because attr is implemented to return nothing when there are no items found. I believe there is a name for this philosophy but I can't think of it.


Your proposed "ignore exceptions" method would only work if all your exceptions were unhandles and could be ignored. This is not the case in general since

  1. Catching an exception is a form of flow control. You wouldn't want to mess up your for-loops, right?

  2. The code following the exception line could very well depend on that line having executed correctly. (for example, someone could want to use that .value)

Therefore, the best you can hope to achieve is to see what kind of exceptions you are currently having and tune your debugging to it. Are you just having problems with elements not being found, as the initial example? Then add a test for null before using the node:

var node = document.getElementById('id');
if(!node) { /* inexisting element!*/ }
console.log('I dont have to worry about', node.value, 'now');

If your exceptions are coming from somewhere else then there is probably a similar solution as well.


You would have to wrap each part within a try...catch trap.

Another alternative, use jQuery as it doesn't throw exceptions when elements are not found:

$('.non-existing-element').val();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜