I am using this kind of code in JavaScript.
if(typeof a[x] != "undefined" && typeof a[x][y] != "undefined" && typeof a[x][y][z] != "undefined") {
a[x][y][z].update();
}
// else do nothing
I just thought that using try catch to implement the above logic would simplify the code and reduce the checking of three conditions each time. (See below)
try {
a[x][y][z].update();
} catch(e) {
}
Is it good to use try catch in such a situation?
Note: If you are wondering why I am checking the three conditions: a[][][] contains references to objects and the references are added dynamically.
Readability matters more, unless you’re in an extremely performance optimized loop or function, it shouldn’t make a difference.
As PleaseStand stated, wrapping the whole thing in a
try/catchwill hide any errors. You could alternatively do something like this, if you still wanted totry/catchor