javascript 如何在jquery插件中添加try catch块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26795668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:30:01  来源:igfitidea点击:

How to add try catch block in jquery plugin

javascriptjqueryjquery-plugins

提问by Mithun Debnath

I have a jQuery plugin.I want to add try catch block for exception handling in my jQuery plug-in.

我有一个 jQuery 插件。我想在我的 jQuery 插件中添加 try catch 块以进行异常处理。

My plug-in

我的插件

$(document).ready(function(){
$('.requiredclass').keyup(function() {


$(this).pluginMethod();

});
});

(function($) {  //i Want try catch block for this part

// jQuery plugin definition

$.fn.pluginMethod = function(e) {

       return this.each(function() {
       var $this = $.this; 
       var som= $this.val();

            //My Code goes here  

        });
};

})(jQuery);

Now if I want to add try catch block then how the plugin will look like? In case of jquery function We do something like this

现在如果我想添加 try catch 块,那么插件会是什么样子?在 jquery 函数的情况下我们做这样的事情

the function

功能

function myFunction() {
//varible declarations
try { 
    //Code goes here
}
catch(err) {  //We can also throw from try block and catch it here
    alert("err");
}
finally {
    //code for finally block
}
}

Now this is the format we know in case of a function.But what will be the format if I want to add exception handling in a plug in? In the plugin from (function($) {the plugin starts then there is $.fn.pluginMethod = function(e) {followed by

现在这是我们在函数情况下知道的格式。但是如果我想在插件中添加异常处理,格式会是什么?在从插件(function($) {的插件开始,然后有$.fn.pluginMethod = function(e) {后跟

       return this.each(function() {`. So where to start the try block and stop it,where to put catch block.Can any one suggest me the format.

If any one have any doubt in understanding the question please let me know,I will try to explain in more detail.

如果有人对理解问题有任何疑问,请告诉我,我会尝试更详细地解释。

回答by shinobi

I don't think i really get your question. You need to be more clear.

我不认为我真的明白你的问题。你需要更清楚。

But is this what you want?

但这就是你想要的吗?

$(document).ready(function(){
    $('.requiredclass').keyup(function() {
    $(this).pluginMethod();
    });
});


try { 
    (function($) {
        //jQuery plugin definition
        $.fn.pluginMethod = function(e) {

           return this.each(function() {
               var $this = $.this; 
               var som= $this.val();
               //your Code goes here
           });
        };

    })(jQuery);
} catch(err) {  //We can also throw from try block and catch it here
    alert("err");
} finally {
    //code for finally block
}

回答by Bhavsar Jay

try {
    //Block of code to try
}
catch(err) {
   // Block of code to handle errors
}

回答by soundhiraraj

Try like this,

试试这样,

 try{
(function($) { 

// jQuery plugin definition

$.fn.pluginMethod = function(e) {

       return this.each(function() {
       var $this = $.this; 
       var som= $this.val();

            //My Code goes here  

        })(jQuery);
}
catch(error)
{
alert(error);
}