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

stop javascript executing when catch error - Stack Overflow

programmeradmin0浏览0评论

I need a function that stop execute javascript when catch error. For example as follow:

function AA()
{
  try{
    executeScript();
  }catch(e){
    //stop execute javascript
  }
}

function executeScript()
{
   throw 'error';
}
function BB()
{
  //some script
}
AA();
BB(); //don't execute when error happening

Has anybody know how to do it? Thanks for help.

I need a function that stop execute javascript when catch error. For example as follow:

function AA()
{
  try{
    executeScript();
  }catch(e){
    //stop execute javascript
  }
}

function executeScript()
{
   throw 'error';
}
function BB()
{
  //some script
}
AA();
BB(); //don't execute when error happening

Has anybody know how to do it? Thanks for help.

Share Improve this question edited Jun 27, 2012 at 6:45 正傑 楊 asked Jun 27, 2012 at 6:37 正傑 楊正傑 楊 1111 gold badge1 silver badge6 bronze badges 1
  • If these answers doesn't satisfy your needs, could you please add more information... – Beygi Commented Jun 30, 2012 at 10:55
Add a comment  | 

3 Answers 3

Reset to default 10

I think if you use a return it should be possible :)

function AA()
{
  try{
  }catch(e){
    //stop execute javascript
    return;
  }
  BB(); //don't execute when error happening
}

function BB()
{
  //some script
}

return like that will just return undefined. You can return something more specific like a string or anything else to be able to have a convenient behaviour when you get this early return.

There are two ways,

  1. Add return statement

     function AA()
     {
       try{
       }catch(e){
         return;
       }
       BB();  
     }
    
     function BB(){   
     }
    
  2. If you want to return from in code before catch calls you can add throw

    function AA() {           
        try{
           javascript_abort();
         }catch(e){
            return;
         }
         BB();  
        }
    
    
     function BB(){   
             }
    
         function javascript_abort(){
            throw new Error('This is not an error. This is just to abort javascript');
         }
    

Also you can use setTimeout if you want code from AA to be redundantly to be executed.

function AA()
{
  try{

  }catch(e){
    return;
  }
  BB(); //don't execute when error happening
  setTimeout("AA()",500);
}

function BB()
{
  //some script
}
发布评论

评论列表(0)

  1. 暂无评论