Update: I’m going to leave it as is: The performance hit of a exception (very rare) is better than the probably performance hit for checking on each operation (common)
I’m trying to support an ‘EstimatedRowCount’ that in one case would be the product of two sub-cursors that are joined together:
estimatedRowCount = left.EstimatedRowCount * right.EstimatedRowCount; return estimatedRowCount;
Of course, if left and right are big enough, this will throw an OverflowException.
Here, I don’t really care if estimatedRowCount is 100% accurate, just big enough to know that this cursor is holding a lot of data.
Right now, I’m doing this:
// We multiply our rowcount Int64 estimRowCount = 0; try { estimRowCount = leftRowCount * rightRowCount; } catch (OverflowException) { // Ignore overflow exceptions estimRowCount = Int64.MaxValue; } return estimRowCount;
Is there a better way to test for overflow operations so I don’t have to do the try{}catch to guard?
This sounds like a good use case for the ‘unchecked’ keyword.
To use, simply wrap your assignment in an ‘unchecked’ block:
Then test to see if the result is negative – if it is, it overflowed:
You’ll need to ensure in this case that neither leftRowCount nor rightRowCount can be negative, but given the context I don’t think that’ll occur.