Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

Jasmyne Boggs
Jasmyne Boggs
4,891 Points

My code is giving me three errors and I need help

Program.cs(27,17): error CS1525: Unexpected symbol catch' Program.cs(28,22): error CS1525: Unexpected symbol{' Program.cs(33,8): error CS1524: Unexpected symbol }', expectingcatch' or `finally' Compilation failed: 3 error(s), 0 warnings

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {

                Console.Write("Enter the number of times to print \"Yay!\": ");
                string input = Console.ReadLine();
            try 
            {
                int count = int.Parse(input);
                if (input <= 0)
                    {
                        Console.Write("You must enter a positive number.");
                    }

                else
                {
                    int i = 0;
                    while(i < count)
                    {
                        i += 1;   
                        Console.WriteLine("Yay!");
                    }
                }
                 catch (FormatException) 
                      {
                        Console.WriteLine("You must enter a whole number.");
                        continue;
                      }
            }
        } 
    }
}

There is a syntax error in your try catch statement - the catch block is within the try bracket {}. You need to move catch outside like this:

try
{               
        ProcessString(s);
}
catch (Exception e)
{
        Console.WriteLine("{0} Exception caught.", e);
}
Steven Parker
Steven Parker
218,813 Points

Byron Injeeli - good reply, you should post it as an answer instead of a comment. That makes it available for voting and potential selection as "best answer". If I were a mod, I'd happily promote it for you.

Jasmyne Boggs
Jasmyne Boggs
4,891 Points

Thank you Byron Injeeli! If I could select you as "best answer", I would.

Steven Parker
Steven Parker
218,813 Points

Jasmyne Boggs — It looks like Byron moved is comment, try again now.

1 Answer

There is a syntax error in your try catch statement - the catch block is within the try bracket {}. You need to move catch outside like this:

try
{               
        ProcessString(s);
}
catch (Exception e)
{
        Console.WriteLine("{0} Exception caught.", e);
}