2010-06-15 68 views
2

这可能有点难以遵循。JavaScript异常/错误处理不工作

我已经有了对象内的函数:

f_openFRHandler: function(input) { 
      console.debug('f_openFRHandler'); 
      try{ 
       //throw 'foo'; 
       DragDrop.FileChanged(input); 
       //foxyface.window.close(); 
      } 
      catch(e){ 
       console.error(e); 
       jQuery('#foxyface_open_errors').append('<div>Max local storage limit reached, unable to store new images in your browser. Please remove some images and try again.</div>'); 
      } 
     }, 

try块调用内:

this.FileChanged = function(input) { 
      // FileUploadManager.addFileInput(input); 
      console.debug(input); 
      var files = input.files; 
      for (var i = 0; i < files.length; i++) { 
       var file = files[i]; 
       if (!file.type.match(/image.*/)) continue; 

       var reader = new FileReader(); 
       reader.onload = (function(f, isLast) { 
        return function(e) { 
         if (files.length == 1) { 
          LocalStorageManager.addImage(f.name, e.target.result, false, true); 
          LocalStorageManager.loadCurrentImage(); 
          //foxyface.window.close(); 
         } 
         else { 
          FileUploadManager.addFileData(f, e.target.result); // add multiple files to list 
          if (isLast) setTimeout(function() { LocalStorageManager.loadCurrentImage() },100); 
         } 
        }; 
       })(file, i == files.length - 1); 
       reader.readAsDataURL(file); 
      } 
      return true; 

LocalStorageManager.addImage电话:

this.setItem = function(data){ 
       localStorage.setItem('ImageStore', $.json_encode(data)); 
     } 

localStorage.setItem如果使用了太多的本地存储,则会引发错误。我想在f_openFRHandler(第一个代码示例)中捕获该错误,但是它将被发送到错误控制台而不是catch块。我尝试下面的代码在我的Firebug的控制台来确保我没有疯,它工作正常,尽管功能嵌套的许多层面:

try{ 
    (function(){ 
     (function(){ 
      throw 'foo' 
     })() 
    })() 
} 
catch(e){ 
    console.debug(e) 
} 

任何想法?

+3

这是一个很好的例子,它提出了一个关于复杂情况的简洁问题。好一个。 – 2010-06-15 08:03:18

+0

啊,我应该看到的。谢谢你们的帮助。 – 2010-06-16 02:38:09

+0

án:如果以下答案之一帮助您解决了问题,请点击答案顶部左边的复选标记以接受其中一个答案。这标志着“回答”的问题。 (http://stackoverflow.com/faq) – 2010-06-16 09:14:07

回答

2
var reader = new FileReader(); 
       reader.onload = (function(f, isLast) { 

可能是你的问题就在那里 - 的的FileReader可能调用的onload异步,而此时你的try/catch不再在范围内。 FileReader接口上可能存在单独的错误处理函数,或者您可能需要将try/catch移动到您传递给onread()的匿名函数中。

2

我认为问题在于您的f_openFRHandler函数完成后,错误发生在稍后。请注意,正在调用LocalStorageManager.addImage的功能在读取器上被设置为onload处理程序,而不是立即调用。它会在数据加载时异步调用。

你需要把你的try..catch匿名函数中被创建并分配给该事件,如:

this.FileChanged = function(input) { 
    // FileUploadManager.addFileInput(input); 
    console.debug(input); 
    var files = input.files; 
    for (var i = 0; i < files.length; i++) { 
     var file = files[i]; 
     if (!file.type.match(/image.*/)) continue; 

     var reader = new FileReader(); 
     reader.onload = (function(f, isLast) { 
      return function(e) { 
       try {  // ADDED 
        if (files.length == 1) { 
         LocalStorageManager.addImage(f.name, e.target.result, false, true); 
         LocalStorageManager.loadCurrentImage(); 
         //foxyface.window.close(); 
        } 
        else { 
         FileUploadManager.addFileData(f, e.target.result); // add multiple files to list 
         if (isLast) setTimeout(function() { LocalStorageManager.loadCurrentImage() },100); 
        } 
       } 
       catch (err) { // ADDED 
        // YOUR HANDLING HERE 
       } 
      }; 
     })(file, i == files.length - 1); 
     reader.readAsDataURL(file); 
    } 
    return true; 
}; 

你的(优秀)测试的情况下,使同步调用,这就是为什么错误的是当你尝试时抓住。这是一个更接近模型所发生的情况:

try{ 
    (function(){ 
     setTimeout(function(){ // Use setTimeout to model asynchronicity 
      throw 'foo' 
     }, 100); 
    })() 
} 
catch(e){ 
    console.debug(e) 
}