Sunday, 3 November 2013

Mismatch in Message property between FileNotFoundException and ArgumentNullException

There is an interesting mismatch in content produced by Message property of FileNotFoundException and ArgumentNullException.

Say, your code does something like following:

throw new FileNotFoundException("Test error", "\\share\test.txt");

If you print the Message property of thrown exception, it prints "Test error" and does not provide any details about the value provided in FilePath parameter (i.e. \\share\test.txt).

On the other hand, if you code does something like following:

throw new ArgumentNullException("param", "message");

If you print the Message property of thrown exception, it prints details about both arguments - message and argument.

Somehow it does not seem consistent. We ran into a bit of problem because of this mismatch. For now, we resorted back to ex.ToString() as that prints the complete information.

No comments:

Post a Comment