Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

try catch throw with doScript()

Community Expert ,
Feb 06, 2012 Feb 06, 2012

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");

}

TOPICS
Scripting
6.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 06, 2012 Feb 06, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 07, 2012 Feb 07, 2012

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");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 06, 2012 Feb 06, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 07, 2012 Feb 07, 2012

@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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 13, 2018 Feb 13, 2018
LATEST

Hi, while attempting to make this script https://www.scriptopedia.org/en/component/phocadownload/file/115-copy-paste-baseline.html backward compatible with InDesign CS5.5 I also tried today some ugly things like that:

try {

    app.doScript(

        run,

        ScriptLanguage.JAVASCRIPT ,

        undefined,

        UndoModes.ENTIRE_SCRIPT,

        "PX_TEMP"

    );

} catch (e) { alert (e || errMsg); }

function run() {

    throw new Error(errMsg = "Im an error");

}

try {

    app.doScript(

        run,

        ScriptLanguage.JAVASCRIPT ,

        undefined,

        UndoModes.ENTIRE_SCRIPT,

        "PX_TEMP"

    );

} catch (e) { alert (e && e.message ? e.message : errMsg); }

function run() {

    throw new Error(errMsg = "Im an error");

}

My excuse being for my part to cancel the whole doScript action after showing temporary to the user the wrong result that he would get with with values out of range:

try { 

    app.doScript( 

        run,  

        ScriptLanguage.JAVASCRIPT ,  

        undefined,  

        UndoModes.ENTIRE_SCRIPT,  

        "PX_TEMP" 

    ); 

} catch (e) { alert (e && e.message ? e.message : errMsg); } 

 

function run() {

    //actions that give wrong results

    if (!confirm ("Look at this mess. Do you want to keep that result anyway?")) {

        throw new Error(errMsg = "Wise decision.");

    }

    alert ("As you wish.");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines