-1

This is my syntax so far:

static void Main(string[] args)
{
    // create a writer and open the file
    System.IO.StreamReader reader =
    new System.IO.StreamReader("c:\\Numbers.txt"); ;
    // open file in Output
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Output.txt"))
    {
        string line;
        int Count = 1;
        while ((line = reader.ReadLine()) != null)
        {
            int y = Int32.Parse(line);
            if ((y % 2) == 0)
            {
                file.WriteLine(" Number " + y + " in line " + Count + " is even ");
            }
            else
            {
                file.WriteLine(" Number " + y + " in line " + Count + " is odd ");
            }
            Count++;
        }
        file.Close();
        reader.Close();
    }
}

I need to invoke exceptions that would output the following lines:

FormatException - Input string was not in a correct format., line No.=10, String=ABC

OverflowException - Value was either too large or too small for an Int32., line No.=11, String=123456789012345678901234567890

FormatException - Input string was not in a correct format., line No.=14, String=4.0

Could anyone help on where and how to write these exceptions.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188

3 Answers3

1

Throw the existing exception System.FormatException or System.OverflowException:

throw new FormatException("my message");

If you only want to display an error message to the user, consider using the message box:

MessageBox.Show("my message");
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

Probably the best way to fix your code is using int.TryParse instead of int.Parse.

http://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

If the input is not a string, probably the easiest way is to try parsing into a decimal: - if still fail--> not a number - else it is a number but not an int

Manuel Spezzani
  • 1,041
  • 7
  • 15
0

My guess would be that you don't want to have exceptions but an output with information about each row containing an error:

while ((line = reader.ReadLine()) != null)
{
    int y;
    if (int.TryParse(line, out y)) {
        if ((y % 2) == 0)
        {
            file.WriteLine(" Number " + y + " in line " + Count + " is even ");
        }
        else
        {
            file.WriteLine(" Number " + y + " in line " + Count + " is odd ");
        }
    } else {
        file.WriteLine(String.Format("Invalid formatted string in line number {0}: {1}", Count, line));
    }

    Count++;
}

Of course you could add some additional checks - but basically you should avoid throwing exceptions if you're kind of expecting some lines to be invalid or if you want to gracefully skip these lines with some comments.

Here's a good answer on when to use exceptions: When to throw an exception?

Community
  • 1
  • 1
mattanja
  • 1,442
  • 14
  • 21
  • Yes, I am expecting lines to be invalid but this project entails outputting exceptions. Thank you though for the input. – user3470207 Mar 27 '14 at 20:36