T
T
Timur Pokrovsky2020-05-19 13:43:50
C++ / C#
Timur Pokrovsky, 2020-05-19 13:43:50

Which is better: handle the exception or not throw it?

Let's say I have a list and what would be the best way to do it? So:
var list = new List<int> { 1, 2, 3, 4 };

int getValue(int index) {
    try {
        return list[index];
    }
    catch (ArgumentOutOfRangeException ex) {
        return -1;
    }
}

or like this:
int getValue(int index) {
    if (index < list.Count) {
        return list[index];
    }
    else {
        return -1;
    }
}

or is there no difference?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2020-05-19
@Makaroshka007

practice measuring execution times .. and questions will disappear
ps for async schemes, everything can change a lot

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question