-4

I'm using a loop to parse a JSONarray.

Suppose, i have 1000 iterations and each of contains a 5 checks:

  try {
          val = e.getString("x");
          if (!val.equals("null"))  //server response, when no data
             tmp.x = Double.valueOf(val);
          else
             tmp.x = 0;
      } catch (Exception e1) {
          tmp.x = 0;
      }

Most of the time catch will not be called, so the question here is to make value checks better (e.g. ==null, isEmpty(), etc...) and remove try-catch, or its fine now?

Orvenito
  • 437
  • 2
  • 14
Vadim
  • 3,855
  • 2
  • 17
  • 22
  • I suggest not to do a loop with such high number of iteration. if you can load it one time and just parse it on one time, it is much better. – Orvenito Sep 28 '17 at 08:20
  • I have like 1000 poins and if use one try-catch outside of loop - if one point fails to parse, then all list will be empty. – Vadim Sep 28 '17 at 08:22
  • no, you just need to have a single identifier to know what points you have to parse. e.g. positive values, >50 points etc.. – Orvenito Sep 28 '17 at 08:25

1 Answers1

1

so the question here is to make value checks better (e.g. ==null, isEmpty(), etc) and remove try-catch, or its fine now?

If you are able to assert that the result that you get from the server is null or not null, then you can use the try-catch block after that validation.

However, as you can see here it will just have a minot impact on your overall performance.

Vanethos
  • 691
  • 1
  • 8
  • 20