4 Replies Latest reply on Feb 7, 2012 12:27 PM by grefel

    try catch throw with doScript()

    grefel Level 2

      Hi,

       

      Today is my InDesign Scripting Bug Day... grrr. In the following example, the Error e in line 6 is undefined. When I run the Function run() without doScript() it works as expected. It looks like, throw isn't implemented correct with doScript(). At least the throw statement works but does not carry the Error. This is not working in CS4 and CS5.5.

       

      try {

          app.doScript(run, ScriptLanguage.JAVASCRIPT , undefined, UndoModes.ENTIRE_SCRIPT, "PX_TEMP");

      //~    run()

      }

      catch (e) {

        $.writeln("Caught: " + e.message);

      }

       

      function run() {

              throw new Error("Im an error");

      }

        • 1. Re: try catch throw with doScript()
          John Hawkinson Level 5

          I would not expect you to be able to throw an error across a doScript(). Did you check the return value?

          • 2. Re: try catch throw with doScript()
            Marc Autret Level 4

            The try...catch structure is very specific in that it creates (and destroys) at runtime its own variable scope to target the Error object. I don't know exactly how app.doScript is implemented but there are some reason to believe that the error thrown in the inner script cannot go back to the outer try...catch scope. The error object is probably lost when the inner script returns.

             

            An ugly solution is to catch the error within app.doScript, to backup the object in a safe place and to re-throw the same error in the outer try block:

             

            function run(){ throw new Error("Im an error"); }
            run.error = null;
            
            try {
                app.doScript('try{ run(); } catch(eInner){ run.error=eInner; }');
                if( run.error ){ throw run.error; }
            
                // you can throw other errors here
                // ...
                }
            catch(e)
                {
                alert( "Caught: " + e );
                }
            

             

            But I hope you have a really good excuse to do such a thing!

             

            @+

            Marc

            • 3. Re: try catch throw with doScript()
              grefel Level 2

              You can perfectly throw an Error through doScript(). The Exception is raised, but the Error-Object does not arrive. A normal Runtime Exception like in the following Example works well. The problem is with the buggy implementation of throw.

               

              try {

                  app.doScript(run, ScriptLanguage.JAVASCRIPT , undefined, UndoModes.ENTIRE_SCRIPT, "PX_TEMP");

              //~    run()

              }

              catch (e) {

                $.writeln("Caught: " + e.message);

              }

               

              function run() {

                  x = x; // this causes an Runtime Error

              //~     throw new Error("Im an error");

              }

              • 4. Re: try catch throw with doScript()
                grefel Level 2

                @Marc As said in the previous post: A normal Runtime Error can go back to the outer try catch scope. But anyway: Your workaround is an interesting piece of code, but i cannot imagine an excuse for that