-2

I have a code that I have to execute with eval() function of JavaScript (If you want to discuss if it is ok or not to use eval() function please add info here: Why is using the JavaScript eval function a bad idea? but do not focus on it here).

After some code it is executed with eval() function, and if it did not gives any exception on the try-catch block, this code will change and I will have to execute again eval() function in another try-catch block.

The problems comes that if I put one try-catch block above the other, they will be executed together and the code will not be clean, so the eval() function it is going to be executed twice. Like this:

var code = some code here;

try{
   eval(code); //Here the code has divs animations
   $("#animate").promise().done(function(){
        checkAnimation();
        //Clear code
   });      
}catch(error){
   console.log(error);
}

try{
   eval(code); //Animations with the same div

   $("#animate").promise().done(function(){
        checkAnimation();
   });  
}catch(error){
   console.log(error);
}

I also saw that there is a finally statement but it will be executed no matter if the try-catch block have made an exception or not so it is not useful for me.

What I think

I think that I can include the second try-catch block after clearing the code but I do not know if it would be a good practise because it is inside another function and the code inside that function can also generate an exception.

My question

Can I execute a try-catch block only when the first try-catch block has worked without exceptions?

EDIT: I edited my code because I forgot to put that in my second try-catch I also had an animation.

Thanks in advance!

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • 1
    Put the entire second `try..catch` inside your `done` callback...!? – deceze Apr 12 '16 at 11:32
  • @deceze but it wont be a bad practise as I put before? Because the 'checkAnimation' function also could gives an exception. – Francisco Romero Apr 12 '16 at 11:36
  • Anything inside the `done` callback won't be handled by your first `try..catch` block! Because it's being executed asynchronously. You will have to establish an entirely new, independent `try..catch` block inside that callback anyway. – deceze Apr 12 '16 at 11:37
  • @deceze The problem it is that the second try-catch block starts before the "Clear code" statement it is being executed. – Francisco Romero Apr 12 '16 at 11:43
  • @deceze and thank you for clarify me to enter another one try-catch to handling "checkAnimation" function. – Francisco Romero Apr 12 '16 at 11:44
  • *"The problem it is that the second try-catch block starts before..."* – That's why you're supposed to put it into the `done` callback... – deceze Apr 12 '16 at 11:47

2 Answers2

0

Anything inside the done callback is a) scheduled to execute after the animation has finished and b) will therefore be independent of the outer try..catch block. You need to put any code you want to execute after // clear code a) into the callback and b) into a new try..catch block:

try {
    eval(code);
    $("#animate").promise().done(function () {
         try {
             checkAnimation();
             //Clear code
             eval(code);
         } catch (error) {
             console.log(error);
         }
    });      
} catch (error) {
    console.log(error);
}

To visualise better how/why the callback is independent of the first try..catch, write the callback as an independent function instead of inline:

try {
    eval(code);
    $("#animate").promise().done(callback);      
} catch (error) {
    console.log(error);
}

function callback() {
     try {
         checkAnimation();
         //Clear code
         eval(code);
     } catch (error) {
         console.log(error);
     }
}
deceze
  • 510,633
  • 85
  • 743
  • 889
  • I knew what you mean when I put "_The problem it is that the second try-catch block starts before..._" because I changed my code as you said, but the problem it is still that the second `eval(code)` it is being executed before the line "`//Clear code`" it is being executed, so the first code it is being executed twice, one in each `eval()` function. – Francisco Romero Apr 12 '16 at 11:58
  • I am sorry but I forgot to put that in the second `try-catch` block I also have animations so I think that the second `.done()` function it is getting that the div it is still moving so it is why it is being executed before the code has been cleared. Look my edited question and sorry again. – Francisco Romero Apr 12 '16 at 12:06
  • Well, yes, in your code you're *immediately* executing `eval` a second time. If you want to delay that execution until after the first animation has finished and the code got "cleared", **you need to put that `eval` call into the asynchronous `done` callback.** – deceze Apr 12 '16 at 12:10
  • Is it the same when you say the `asynchronous done callback` as the function that you have used before? Sorry so much if it is a beginner question. – Francisco Romero Apr 12 '16 at 12:23
  • `$("#animate").promise().done(/* HERE */)` ← Whatever goes `/* HERE */` is your *done callback*. And it will be called *asynchronously*, which means *sometime later*, after all this `try..catch` code is long done. – deceze Apr 12 '16 at 12:27
  • Thank you for the explanation but the problem it is that, as the `eval(code)` function it is the function that executes the animation and I need that this animation finish (it is why I make `promise().done()` before checking it, I think I cannot put the `eval(code)` function on the the second done callback function because it had to eval before wait and in the first one I think it does not have sense because the code is still not clear. I expect you could understand what I mean. – Francisco Romero Apr 12 '16 at 13:09
0

Finally, I solved it like follows:

var count = 0;

function evaluateCode(){
     try {
        eval(code);

        $("#animate").promise().done(function(){
           if(count == 0){
              checkAnimation1(); //I clear the code inside this function
           }
           else{
              checkAnimation2();
           }

           count++;
        });         
     } catch (e) {
        alert(e);
     }
}

And I called this function twice. If I did not do like this, the code never clear before executing the next eval(code).

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167