0

This application tests hardware and prints the results of the test to the console. Multiple devices can be under test at once so I have multiple threads and locking around access to the console, both input and output. So I originally had this in my code right before passing the result to the function that prints:

string message = String.Format("The DUT is: {0}. The total test " +
"was a : {2}.", MAC, testResultString);

The 2 caused the application to stop executing that function. It switched control back to the other threads but never complained about an error etc so the problem took quite a while to track down. What are good strategies/best practices for dealing with String.Format since it is apparently pretty quiet when there is a problem. Or alternatives to string format that have similar flexibility.

Edit: yes the bug was tracked down and the code changed to:

string message = String.Format("The DUT is: {0}. The total test " +
"was a : {1}.", MAC, testResultString);

The point of the question is moreso how to deal with String.Format silently failing. As correctly pointed out by @alexd, this is not a problem specific to String.Format. Any function in a separate thread that throws an exception will have the same issue.

Thanks for the pointers on Re-sharper and the edits @VirtualBlackFox.

Community
  • 1
  • 1
therealjumbo
  • 1,079
  • 1
  • 10
  • 14

6 Answers6

2

As Daniel James Bryars already said, meet ReSharper:

enter image description here

2 Warnings on this line as the second parameter is never used in the format string (And one error due to missing ;).

You can even with an attribute mark your own code or external code like NLog with this feature.

Warning are aggregated on the scrollbar as colored lines, available on a separate window and it can be integrated in nearly any automated system (Sonar for example)

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
1

This will throw an error because {2} actually refers to the third parameter after the string. Since you only have two, it throws an exception.

string message = String.Format("The DUT is: {0}. The total test " +
"was a : {1}.", MAC, testResultString);

As long as your token references don't exceed your parameter count, you should not have to worry about error handling on a String.Format.

wizulus
  • 5,653
  • 2
  • 23
  • 40
1

The problem is not specific to String.Format. Pretty much any exception, thrown from a background thread, will lead to the same problem.

You may consider AppDomain.UnhandledException to catch and report such exceptions:

AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
    Exception x = (Exception)e.ExceptionObject;
    // report error, etc.
};

But there are quite some details to be aware of, see http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception%28v=vs.110%29.aspx.

P.S. This page provides a good overview of possibilities: WPF global exception handler

Community
  • 1
  • 1
AlexD
  • 32,156
  • 3
  • 71
  • 65
0

If your using VS then you may need to enable those exceptions under the debug menu. String.Format does throw an exception if the number of arguments is less than any index used. Look at the MSDN page.

Update: more specifically, you need to enable exceptions from the CLR (Common Language Runtime).

John Yost
  • 693
  • 5
  • 20
0

Why don´t you try something like:

string message = String.Format("The DUT is: {0}. The total test was a : {1}.", MAC, testResultString);

I hope this helps! And SLaks says in the comment! Add a try/catch to check the exceptions ;)

Oscar Bralo
  • 1,912
  • 13
  • 12
0

In C#6 you can now use "String interpolation" (see here and here) and do something this:

string message = $"The DUT is: {MAC}. The total test " +
"was a : {testResultString}.";

and I don't know if this is compile time checked.

Community
  • 1
  • 1
Daniel James Bryars
  • 4,429
  • 3
  • 39
  • 57