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;
                      }
            }
        } 
    }
}
Byron Injeeli
Byron Injeeli
Full Stack JavaScript Techdegree Student 19,135 Points

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
173,904 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
173,904 Points

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

1 Answer

Byron Injeeli
Byron Injeeli
Full Stack JavaScript Techdegree Student 19,135 Points

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);
}