-2

I read this article Multiple try-catch or one? and was wondering if there was a better way? Is there a way to just ignore the bad lines of code? My problem is I need to load multiple variables from objects that may or may not exist. Thanks everyone

 toparcv = document.getElementsByName("attribute[425]")[0].value;
  toparcv = document.getElementsByName("attribute[423]")[0].value;
  toparcv = document.getElementsByName("attribute[424]")[0].value;
  toparcv = document.getElementsByName("attribute[426]")[0].value;
  toparcv = document.getElementsByName("attribute[434]")[0].value;
  bottomarcv = document.getElementsByName("attribute[271]")[0].value;
  bottomarcv = document.getElementsByName("attribute[265]")[0].value;
  bottomarcv = document.getElementsByName("attribute[268]")[0].value;
  bottomarcv = document.getElementsByName("attribute[369]")[0].value;
  bottomarcv = document.getElementsByName("attribute[433]")[0].value;
console.log(toparcv);
console.log(bottomarcv);

im trying to read a textbox from a website that randomly generates the name from about 10 different names by adding 433 or 268.

Community
  • 1
  • 1
  • 1
    It sounds like you should be using an `if` statement to check that the object exists. – 4castle Oct 10 '16 at 03:23
  • Put `try/catch` just around the code that throws the exception, instead of the whole block. – Barmar Oct 10 '16 at 03:26
  • Maybe you should be using a loop or function so you don't need to repeat code for each variable. – Barmar Oct 10 '16 at 03:27
  • the if statement would have like 15 if else's or am i nuts? – zack mcdonald Oct 10 '16 at 03:32
  • good idea on the loop maybe i can do that – zack mcdonald Oct 10 '16 at 03:32
  • 1
    @zackmcdonald - can you provide some code showing exactly what it is you are trying to do, or an example? It seems like maybe you've got a problem that's easily solved but you're just going down the wrong path with the `try...catch` stuff... – Alexander Nied Oct 10 '16 at 03:34
  • I agree with anied. If you show us an exact example of what you're trying to do, we can help you much, much more specifically. You have proposed [an XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), where you're asking questions about your proposed solution rather than explaining the problem and letting us offer the best solution. – jfriend00 Oct 10 '16 at 03:37
  • i cant put try catch around the whole code, it will end the code after the first bad line – zack mcdonald Oct 10 '16 at 04:31

1 Answers1

0

You can cover all the bottommarcv assignments like this:

[271, 265, 268, 369, 433].forEach(function(num) {
    var list = document.getElementsByName("attribute[" + num + "]");
    if (list && list.length) {
        bottomarcv = list[0].value;
    }
});

This code pre-preemptively avoids an exception by checking the integrity of variables before referencing properties on them and because it's using a loop to examine each element, it can use only one if statement for all the values.

It is a little odd to be assigning them all to the same variable. Do you really only want the last one that has a value to be used here?

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @zackmcdonald - You will have to show us exactly what code you are using because I don't think the code in my answer can generate that error. If `list` is `undefined`, then the code will not evaluate `list.length` at all. Please check to make sure you have the code the same as I show it. – jfriend00 Oct 10 '16 at 03:56
  • @zackmcdonald - Did this work for you once you corrected your code? – jfriend00 Oct 10 '16 at 16:29