Try-Catch or Check Length?

c#, try-catch

Solution

Checking the length is a much cheaper operation than catching an exception. When you have a try..catch block, it's adding extra structures into your code for catching exceptions - which is fine, I don't say it's wrong, but if you can check the length of the bounds then do that instead.

Problem

I was just wondering which would be cheaper, using a try catch block for index out of bounds or checking the length of a multi dimensional array and comparing values? I have a feeling it's the length, since I can store the length in a variable and then just do if's which are relatively cheap. I'm just not sure how expensive try-catch is. Thanks!

Original source

Related problems