Dashzapp
Dashzapp

Reputation: 143

Filtering debug output for C#

Is it possible to filter out different sorts of exceptions out of my debug output in C#?

I want to filter out 'System.FormatException', because I know it's going to occur, and it gives a rubbish oversight of my output. I'm scanning a textfile with over 20,000 lines, and almost a quarter of them are wrong, but I don't want 'System.FormatException' 5000 times in my output...

The code is seen below, and you can see, if it's not a number, it will not double.parse, so it will catch the error.

if (!(dataline.EndsWith(";0") || intCounter == 0))
{
    try
    {
        natMB = double.Parse(splitline[8], NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
    }
    catch
    {
        natMB = 0;
    }

    double intMB;
    try
    {
        intMB = double.Parse(splitline[9], NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
    }
    catch
    {
        intMB = 0;

Upvotes: 2

Views: 1677

Answers (3)

Cake
Cake

Reputation: 149

If you want to ignore this exception while debugging, you can disable the catch of the exception.

In the menus, go to: DebugExceptions... (in my Visual Studio, the hotkeys are Ctrl + D, E).

Then click on "Find...", and search your exception. When you found it, make sure none of the checkboxes are checked. It should ignore it now.

Upvotes: 2

Liath
Liath

Reputation: 10191

I find the built-in .NET logging mechanisms frustrating for this sort. I would take a look at something like log4net or NLog which give you a large amount of control over what level is logged and in which namespaces.

Combine these with Log2Console for a live logging trace.

Upvotes: 2

beta
beta

Reputation: 2633

If you'd like to ignore an exception you could just put the following try/catch over it:

try
{
  // Insert your code here
}
catch(System.FormatException)
{

}

Seeing your code I recommend you to use

double d = 0;
Boolean success = double.TryParse(splitline[8], out d);

if(success)
  Console.WriteLine("Conversion successful!");
else
  Console.WriteLine("Damnit.");

instead. This will return a boolean on whether the conversion went fine or not and also will store the parsed double in the variable you passed the function.

Upvotes: 7

Related Questions