🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #8451

JavaScript Error Handling Question

#8451
From: Jim_Birkenseer
Date: Fri, 13 Oct 2006 23:00
46 lines
3088 bytes
Hi Gang,

I'm working on an ID CS2 JavaScript that looks for a certain attribute in every XMLElement in a document. In my effort to determine the most efficient method (execution-time-wise), I came across a result that I didn't expect. The problem happens when I try to get an attribute from an element, and the attribute doesn't exist.

To get the error to happen with the code snippet below, create or open an InDesign CS2 document that does not have an XMLAttribute called "test" in the Root XMLElement. With that document as the active document, call the following the function from a JavaScript in the ExtendScript Toolkit.

function XMLAttributeTest()
{
    var theRootEle;
    var theAttr;
    var theAttrValue;

    theRootEle = app.activeDocument.xmlElements[0];
    theAttr = theRootEle.xmlAttributes.item("test");

    try
    {
        theAttrValue = theAttr.value;
        alert("The value = " + theAttrValue);
    }
    catch(theErr)
    {
        // This is never hit
        alert("Caught an error.");
    }
}


So, since there is no "test" attribute assigned to the Root node, theAttr doesn't contain a valid xmlAttribute, which I expected. However, in this situation, I expected that an error would occur in the try block when I tried to read theAttr's value property, and I would catch it in the catch block. The error reported by ExtendScript Toolkit is...

The requested action could not be completed because the object no longer exists.


...however, this error is not caught.

I'm new to JavaScript, and perhaps this is the correct behavior of the try context; but I figured if there was an error in the try context, I would be able to catch the error in the catch block.

Additional information: I discovered that, in the error case, theAttr is not NULL and it is still considered an XMLAttribute when checking its constructor.name. Due to my lack of JS experience, I don't know if there are other ways of determining if theAttr is valid.

In the meantime, I can do what I need in JavaScript by checking to see if there are any XMLAttributes for the XMLElement, and if there are, I compare each attribute's name with the name of the attribute I'm looking for. I was hoping that there might be a speed improvement if I had InDesign try to directly find the attribute for me, but I ran into the issue stated above. Since I'm going to be working through thousands of XMLElements, I'm looking for any speed improvements I can.

Thanks in advance for any guidance!!

Cheers!

-- Jim
Technical Details
Message-ID: <3bc1f75d.-1@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!news.ecp.fr!news.stben.net!webcrossing

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,3b05154e3909ab65
X-Google-Attributes: gidf939a,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Jim_Birkenseer@adobeforums.com
Newsgroups: adobe.scripting.indesign
Subject: JavaScript Error Handling Question
Message-ID: <3bc1f75d.-1@webcrossing.la2eafNXanI>
Date: Fri, 13 Oct 2006 16:00:26 -0700
Lines: 70
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!news.ecp.fr!news.stben.net!webcrossing