TimeSpan.Parse breaking change in .Net Framework 4
Posted
Wednesday, October 13, 2010 6:40 PM
by
ysa
This week my team upgraded our solutions to Visual Studio 2010 and .Net framework 4. When we ran the tests of one our projects, one of our tests failed with a weird reason. The code is simple:
1: public DateTime CreateDateWithGivenTime(string time)
2: {
3: var t = TimeSpan.Parse(time);
4: ...
5: }
The test was:
1: [Test]
2: [ExpectedException(typeof(OverflowException))]
3: public void CreateDateWithGivenTime_HoursNotInRange_ThrowOverflowException()
4: {
5: CreateDateWithGivenTime("77:11:00");
6: }
Testing on a branch before the conversion – the test passed.
Debugging the code in both cases showed different behavior of the framework:
Framework 3.5: Throws exception
Framework 4: Code recovers and parses the string as if “77” is days, “11” is hours etc…
Apparently, according to msdn, TimeSpan.Parse behaves differently in framework 4. I was very amused to see how the writer avoids the word Breaking change. The docs indicates that in certain cases where in .Net framework 3.5 Parse throws an exception, in framework 4 all is good and vice versa.
Lucky we have tests…