Check if adding TimeSpans exceeds TimeSpan.MaxValue

Viewed 1614

I am trying to add some TimeSpans together for some calculations.
eg:

var timeSpan1 = new TimeSpan.FromMinutes(1);
var timeSpan2 = new TimeSpan.MaxValue; 
var timeSpan3 = timeSpan1 + timeSpan2;

In this case it will exceed the TimeSpan.MaxValue and throw an exception

System.OverflowException : TimeSpan overflowed because the duration is too long.

What is the correct way to add TimeSpans?

Do I have to put a try-catch around it or write my own extension method? Seems like this should be part of the .Net framework - either default to MaxValue or have a TryAdd method.

2 Answers
Related