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

chris burnside
chris burnside
894 Points

My code will run in the workspaces but i keep getting the error "Bummer! Did you catch FormatException thrown by int.Par

What am i missing here. I already have included a the try/catch with the format exception

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");    
                var entry = Console.ReadLine();
                var number = int.Parse(entry);
                var count = 1;

            try
            {
                while(count<=number)
                {
                 if (number<=0 && count<=0)
                    {
                        Console.WriteLine("You must enter a whole number");
                        break;
                    }    
                else if (count>0)
                    {
                        Console.WriteLine("Yay");
                        count += 1;

                    }
                }
             }
             catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");  
                }
        }
    }
}

3 Answers

Allan Clark
Allan Clark
10,810 Points
var number = int.Parse(entry);

this is the line that possibly throws an exception, just move it down a few lines into the try block

Jonathan Muniz
PLUS
Jonathan Muniz
Courses Plus Student 3,923 Points

The issue is that your Parse is not in the try block and thus the program isn't looking for the FormatException for Parse because catch will only look for the exception of what's within the try block. Moving the Parse into the try block should fix it.

chris burnside
chris burnside
894 Points

Thank you! i have completed the final!!!! that held me up for a couple hours!