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

javascript - How to add try catch block in jquery plugin - Stack Overflow

programmeradmin2浏览0评论

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

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

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

       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.

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

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

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

       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.

Share Improve this question edited Nov 7, 2014 at 7:22 kaxi1993 4,7004 gold badges31 silver badges48 bronze badges asked Nov 7, 2014 at 6:50 Mithun DebnathMithun Debnath 5881 gold badge8 silver badges24 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

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) {  
    alert("err");
} finally {
    //code for finally block
}

    

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);
}
try {
    //Block of code to try
}
catch(err) {
   // Block of code to handle errors
}
发布评论

评论列表(0)

  1. 暂无评论