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

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
229,644 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
229,644 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);
}