5

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.

1
  • If these answers doesn't satisfy your needs, could you please add more information... Commented Jun 30, 2012 at 10:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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');
         }
    

Comments

1

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
}

2 Comments

sometimes you need some code to be executed until an event is triggered.
I really don't see what that has to do with the code you posted -- or what that has to do with the question =/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.