C# exception handling question

Discussion in 'Off Topic' started by Danja, May 16, 2010.

Thread Status:
Not open for further replies.
  1. Danja

    Danja Notebook Evangelist

    Reputations:
    57
    Messages:
    576
    Likes Received:
    0
    Trophy Points:
    30
    Hi all,

    I decided to try to learn C# over the summer. My last programming experience was c++ in a high school class 4 years ago so it's taking me a while to get back into the swing of things. I'm using the C# station tutorial as a guide but I decided to modify some of the code for one of the lessons using basic skills from C++. I have the following snippet of code and it's giving me a Syntax Error, 'try' expected.

    Console.WriteLine("Please input the initial value of x");
    while (xtracker == false)
    {

    inputx = Console.ReadLine();
    try
    {
    x = Convert.ToInt32(inputx);
    xtracker = true;
    }
    catch (FormatException)
    {
    Console.WriteLine("You can't do arithmetic with letters, ");
    Console.WriteLine("Please input the initial value of x");
    xtracker = false;
    }

    }
    Console.WriteLine("Please input the initial value of y");

    while (ytracker == false)
    {

    inputy = Console.ReadLine();
    try
    {
    y = Convert.ToInt32(inputy);
    ytracker = true;
    }
    catch (FormatException)
    {
    Console.WriteLine("You can't do arithmetic with letters, ");
    Console.WriteLine("Please input the initial value of y");
    ytracker = false;
    }
    }

    If I just use the stuff about x and leave my y variable without exception handling, everything goes smoothly and the program runs as intended. However, when I do the exact same exception handling for y, I get a Syntax error, 'try' expected immediately after the very last bracket in the snippet.

    Could someone please explain why this is so?

    Thanks
     
  2. swarmer

    swarmer beep beep

    Reputations:
    2,071
    Messages:
    5,234
    Likes Received:
    0
    Trophy Points:
    205
    What are the next lines of your program after the snippet you posted?
     
  3. Danja

    Danja Notebook Evangelist

    Reputations:
    57
    Messages:
    576
    Likes Received:
    0
    Trophy Points:
    30
    Here is the full text of the program. I realize it's long and messy; I haven't learned how to call functions in other .cs files yet the way I could with C++ so this is the best I can do for now.

    using System;

    public class Math
    {
    public static void Main()
    {
    int x, y, result, trackinitialinput;
    float floatresult;
    bool looptracker, ytracker, xtracker;
    string inputx, inputy, yn;

    x = 0;
    y = 0;


    looptracker = true;
    ytracker = false;
    xtracker = false;

    Console.WriteLine("This program will perform basic interger operations with C# syntax");
    Console.WriteLine("Two variables, x and y, will be used.");
    Console.WriteLine("Please input the initial value of x");
    while (xtracker == false)
    {

    inputx = Console.ReadLine();
    try
    {
    x = Convert.ToInt32(inputx);
    xtracker = true;
    }
    catch (FormatException)
    {
    Console.WriteLine("You can't do arithmetic with letters, ");
    Console.WriteLine("Please input the initial value of x");
    xtracker = false;
    }

    }
    Console.WriteLine("Please input the initial value of y");

    while (ytracker == false)
    {

    inputy = Console.ReadLine();
    try
    {
    y = Convert.ToInt32(inputy);
    ytracker = true;
    }
    catch (FormatException)
    {
    Console.WriteLine("You can't do arithmetic with letters, ");
    Console.WriteLine("Please input the initial value of y");
    ytracker = false;
    }

    }



    finally
    {
    }
    trackinitialinput = 0;

    while (looptracker != false)
    {
    if (trackinitialinput == 0)
    {
    }

    if (trackinitialinput != 0)
    {
    Console.WriteLine("Please enter the new value for x");
    inputx = Console.ReadLine();
    try
    {
    x = Convert.ToInt32(inputx);
    }
    catch (FormatException)
    {
    Console.WriteLine("You can't do arithmetic with letters, ");
    Console.WriteLine("Please input the initial value of x");
    inputx = Console.ReadLine();
    }

    Console.WriteLine("Please enter the new value for y");
    inputy = Console.ReadLine();
    try
    {
    y = Convert.ToInt32(inputy);
    }
    catch (FormatException)
    {
    Console.WriteLine("You can't do arithmetic with letters, ");
    Console.WriteLine("Please input the initial value of y");
    inputx = Console.ReadLine();
    }
    }

    result = x + y;
    Console.WriteLine("X+Y: {0}", result);

    result = x - y;
    Console.WriteLine("X-Y: {0}", result);

    result = x * y;
    Console.WriteLine("X*Y: {0}", result);

    result = x / y;
    Console.WriteLine("X/Y (int): {0}", result);

    floatresult = (float)x / (float)y;
    Console.WriteLine("X/Y (float): {0}", floatresult);

    result = x % y;
    Console.WriteLine("X%Y: {0}", result);

    result += x;
    Console.WriteLine("result+=X: {0}", result);

    result += y;
    Console.WriteLine("result+=Y: {0}", result);

    Console.WriteLine("\n");

    Console.Write("continue? (y/n)");
    yn = Console.ReadLine();
    if (yn == "y")
    { looptracker = true;}
    else
    { looptracker = false; }



    x++;
    y++;
    trackinitialinput++;
    }

    Console.WriteLine("This computer will now begin self destruct sequence. Please save any important data to an external disk.");
    Console.ReadLine();

    }
    }



    I have another question that's unrelated to my first one but I'll just shove it here. I know how to handle incorrect input type exceptions, but how do I handle when an input is prompted and the user just hits "enter" without adding any input?

    Thanks!
     
  4. swarmer

    swarmer beep beep

    Reputations:
    2,071
    Messages:
    5,234
    Likes Received:
    0
    Trophy Points:
    205
    ok, I see the problem. The "finally" section has to immediately follow try/catch... but in your code you have an extra close-braces to close the while loop before the "finally".

    However, if the finally block is empty, then you shouldn't need it at all. So you can just take out the "finally" stuff entirely.

    It's probably already being caught, since it probably can't convert to an integer if no text is entered.

    But if you want to check for it specially (to give a different error message, for example)... I don't know all the C# library functions off the top of my head, but you can trim whitespace from the string (using a trim function or something like that) and then check if the length of the string is greater than 0.
     
  5. Danja

    Danja Notebook Evangelist

    Reputations:
    57
    Messages:
    576
    Likes Received:
    0
    Trophy Points:
    30
    Thanks for the help! NBR says I've already given you rep recently; otherwise I would rep you again. Thanks!
     
  6. Thaenatos

    Thaenatos Zero Cool

    Reputations:
    1,581
    Messages:
    5,346
    Likes Received:
    126
    Trophy Points:
    231
    Just felt like quoting :)
     
Thread Status:
Not open for further replies.

Share This Page