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: https://graphical.weather.gov/xml/SOAP_server/ndfdXMLclient.php?zipCodeList=90210&product=glance&begin=2004-01-01T00:00:00&end=2018-12-05T00:00:00&Unit=e&sky=sky&Submit=Submit

A bad ZIP code throws an error, obviously: https://graphical.weather.gov/xml/SOAP_server/ndfdXMLclient.php?zipCodeList=00000&product=glance&begin=2004-01-01T00:00:00&end=2018-12-05T00:00:00&Unit=e&sky=sky&Submit=Submit

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!

Answers

  •         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 [Lprocessing.data.XML;@3d986b90 <value>84</value>

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

    loading xml... from if, cloud cover is [Lprocessing.data.XML;@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 ✓

    @koogs

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

    Kf

  • Thanks, Kf! :-)

Sign In or Register to comment.