最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Do not stop JavaScript when it throws an exception - Stack Overflow

programmeradmin1浏览0评论

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 every 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.

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 every 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.

Share Improve this question asked Jul 29, 2011 at 19:26 1' OR 1 --1' OR 1 -- 1,7142 gold badges19 silver badges32 bronze badges 5
  • Why would you want to do this? I would seriously re-consider your approach. – James Montagne Commented Jul 29, 2011 at 19:30
  • 3 Why not do proper error checking instead of catching exceptions, like this jsfiddle? It's worth pointing out that exceptions are supposed to be... you know... exceptional. They're supposed to cause the application to halt in the general use case. – Nick Husher Commented Jul 29, 2011 at 19:33
  • The non-existing element was only an example. There could be any code that throws an exception in place of it. – 1' OR 1 -- Commented Jul 29, 2011 at 19:35
  • Birk check out my answer about error suppression. It seems closer to what you may be looking for. – Utilitron Commented Jul 29, 2011 at 19:54
  • see also: stackoverflow.com/questions/24318654 – dreftymac Commented Jun 20, 2014 at 1:43
Add a comment  | 

5 Answers 5

Reset to default 8

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)

  1. 暂无评论