Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect Final

Stepas Loiba
Stepas Loiba
11,180 Points

Hello,i looking for help adding formatexeption(try,catch),would like to have some opinions where start try{},thanks.

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { int index = -1;

        Console.Write("Enter the number of times to print \"Yay!\": ");
        var entry = Console.ReadLine();
        var times = int.Parse(entry);
        while (true)
        {

            int value = ++index;

            if (value == times)
            {
                break;
            }



            Console.WriteLine("Yay!");
        }
    }
}

}

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            int index = -1;

            Console.Write("Enter the number of times to print \"Yay!\": ");
            var entry = Console.ReadLine();
            var times = int.Parse(entry);
            while (true)
            {

                int value = ++index;

                if (value == times)
                {
                    break;
                }



                Console.WriteLine("Yay!");
            }
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,269 Points

:point_right: The try section must cover the code where the conversion is done.

So at a minimum, it must enclose the line with the int.Parse call. It may optionally cover more lines, for your convenience.

The catch section should immediately follow the try block. Remember that if there is an exception, the program should print the error message and then end. This is why it might be handy to put more of the program (perhaps all of the existing parts) inside the try block.

Stepas Loiba
Stepas Loiba
11,180 Points

thank you Steve.very useful and constructive feed back.