When should you use try/catch in JavaScript?

后端 未结 7 1069
旧时难觅i
旧时难觅i 2021-01-30 00:53

When I\'m developing normal web application with JavaScript, the try/catch statement is not needed usually. There\'s no checked exception, File IO or database conne

7条回答
  •  你的背包
    2021-01-30 01:39

    Well, I personally (mis?)use it, when I write some code which I'm not sure will execute properly, but the user don't need to know about the error.

    Other than that, I've been using it on some user controls, to which you can define an 'action' property in your HTML markup, and the javascript will try executing that action, like this:

    try{
         window['method'](args);
    }catch(e){
         try{
             window['optionalExceptionHandler'](e, args);
         }catch(e){ return; }
    }
    

    (I like to think it's better than eval() xD)

提交回复
热议问题