Trap for XML Array Out of Bounds exception?

edited May 2017 in Questions about Code

Hi folks -- really simple one here, I think. I’m doing a lookup via the NOAA API for a user-entered ZIP code.

A real ZIP code is fine:

A bad ZIP code throws an error, obviously:

What I can’t figure out how to do is trap for that error! I was hoping that try/catch would work...

try { cloudCover = xml.getChildren("data/parameters/cloud-amount/value"); } catch (Exception e) { println(e.getMessage()); return; }

...but no luck (it doesn’t println anything) -- even though getChildren works when the ZIP code is valid, and it throws an Array Out of Bounds when passed a bad ZIP code.

Presumably a SOAP error needs to be trapped differently? THANKS!


  •         try {
                cloudCover = null; 
                cloudCover = xml.getChildren("data/parameters/cloud-amount/value");
                if(cloudCover != null) { 
                       // success


  • edited May 2017

    Whoops, I marked that as "Did not answer" too soon! Sorry, Chrisir -- even though I don’t understand it AT ALL, it does work.

    If I have a valid ZIP, I get this in my console (my printlns):

    loading xml... from if, cloud cover is [;@3d986b90 <value>84</value>

    If I DON’T have a valid value, I get this:

    loading xml... from if, cloud cover is [;@1e6ef2b7 exception? 0

    Now, that from if line is in the SAME PLACE (where //success is in your code snippet) -- so the content ISN’T null (which I suspected back when I started this), BUT now it records the error -- so I can use the exception to ask the user to try a real ZIP code.

    THANKS! Please reply again so that I can mark this as answered. :-3

  • Answer ✓


    @sca610 I forum moderator can mark this post as answered for you.


  • Thanks, Kf! :-)

Sign In or Register to comment.