ExtendScript exception handling is broken

Discussion of actual or possible Photoshop Scripting Bugs, Anomalies and Documentation Errors

Moderators: Tom, Kukurykus

eddy

ExtendScript exception handling is broken

Post by eddy »

It appears to me that the JavaScript ExtendScript exception handling is seriously broken. Try this simple example:

Code: Select allf1 = 0;
f2 = 0;

function TryCatchFinallyDemo(x) {
     try {
        try {
          if (x == 0) 
             throw "x equals 0";           
         else
             throw "x does not equal 0";   
        } catch(e) {                         
           if (e == "x equals 0")         
              return(e + "\nhandled in inner catch block.");
          else                               
              throw e;                         
        } finally {
          f1++;
        }                                     
     } catch(e) {                               
        return(e + "\nhandled in outer catch block.");
     } finally {
       f2++;
     }
  }

alert (TryCatchFinallyDemo(0) + '\ncount=' + f1 + ',' + f2);
alert (TryCatchFinallyDemo(1) + '\ncount=' + f1 + ',' + f2);

In ExtendScript, the alerts (incorrectly) appear as:

undefined
count=0,1

and

x does not equal 0
handled in outer catch block.
count=0,1

Running the exact same code copy-and-pasted into Firefox's JavaScript Console gives the expected and correct results:

x equals 0
handled in inner catch block.
count=1,1

and

x does not equal 0
handled in outer catch block.
count=2,2

A couple things to notice:

1) ExtendScript is not always executing the finally statements, which are supposed to be guaranteed to run, even if an exception is thrown and handled by a parent catch block, the function returns from the catch block, etc.

2) Values are not returned inside nested catch statements (hence the 'undefined' in the first alert).

There may be other issues as well.
Andrew

ExtendScript exception handling is broken

Post by Andrew »

Hi Eddy

This is interesting and important. I tested your code and got exactly the same results. I have not have time to think through the logic of it yet, my mind is too full of a project I am working on, but thanks for reporting this.

Andrew
RagsGardner

ExtendScript exception handling is broken

Post by RagsGardner »

Eddie,

Very interesting.

I have also seen much weirdness in UI dialog exceptions. Errors showing up in a different scope than where they actually occurred. Often ignored even when the code is in a try/catch. Then showing up elsewhere as undefined “uncaught” exceptions.

I just don’t have any elegant code fragments to demonstrate it. I suspect they are all related.