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 "try" Code and "catch" Exceptions

not sure what I'm doing wrong and how to fix (compiler error)

using System; namespace Treehouse.FitnessFrog { class Program { static void Main() { int runningTotal = 0; bool keepGoing = true;

   while (keepGoing)
   {
    //prompt the user for minutes exercised
    Console.Write("enter how many minutes you exercised or type \"quit\" to exit: ");
    string entry = Console.ReadLine();

     if (entry == "quit")
     {
       keepGoing = false;
     }
     else
    {
       try
         {
         int minutes = int.Parse(entry);

       if (minutes <= 0)
       {
         Console.WriteLine(entry + "is not an acceptable value.");
         continue;
       }
       else if(minutes <= 10)
       {
         Console.WriteLine("Better than nothing, am I right?");
       }
       else if(minutes <= 30)
       {
         Console.WriteLine("Way to go hot stuff!");
       }
       else if (minutes <= 60)
       {
         Console.WriteLine("You must be a ninja warrior in training!");
       }
       else 
       {
         Console.WriteLine("Okay, now you're just showing off!");
       }

       catch(FormatException)
       {
         Console.WriteLine("That is not valid input.");
         continue;
       }

      //Add minutes exercised to total
      runningTotal = runningTotal + minutes;

     //Display total minutes exercised to the screen
     Console.WriteLine("You've entered " + runningTotal + " minutes");
     }
     }
    //Repeat until the user quits
   }

   Console.WriteLine("Goodbye");
 }

} }

In your example 'catch' is INSIDE 'try' because you position the '}' wrong. try and catch should have separate open and closing curly brackets.

'catch' code block could also be after the runningTotal block. Like this:

                    // Add minutes exercised to total 
                  runningTotal = runningTotal + minutes;

                    // Display total minutes exercised to the screen 
                  Console.WriteLine("You've entered " + runningTotal + " minutes.");

                    // Repeat until user quits
                   }
                catch(FormatException) {
                Console.WriteLine("This is a invalid input, please try again.");
                continue;
                }

My program looks like this:

using System;

namespace TreeHouse.FitnessFrog 
{
    class Program
    {
        static void Main()
        {

          int runningTotal = 0;
          bool KeepGoing = true;


          while(KeepGoing) {
              // Prompt user for minutes exercised 
            Console.Write("Enter how many minutes you exercised or \"quit\" if you want to leave the program: ");

            string entry = Console.ReadLine();

              if(entry == "quit") 
              {
              KeepGoing = false; 
              }

              else {

                try {

                  int minutes = int.Parse(entry);

                  if(minutes <= 0) {
                    Console.WriteLine(minutes + " is not a valid amount of time. Please try again");
                    continue;
                  }
                  else if(minutes <= 10) {
                    Console.WriteLine("Better than nothing am i right?");
                  }
                  else if(minutes <= 30) {
                    Console.WriteLine("That's insane. You must be a Ninja Warrior or something.");
                  }
                  else if(minutes <= 60) {
                    Console.WriteLine("By boards Beard! I can't beliave it");
                  }
                  else {
                    Console.WriteLine("Okay, now you're just showing off!");
                  }
                    // Add minutes exercised to total 
                  runningTotal = runningTotal + minutes;

                    // Display total minutes exercised to the screen 
                  Console.WriteLine("You've entered " + runningTotal + " minutes.");

                    // Repeat until user quits
                   }
                catch(FormatException) {
                Console.WriteLine("This is a invalid input, please try again.");
                continue;
                }
             }

          }
          Console.WriteLine("GoodBye!");

        }
    }
}

Hope this could help.

=)

1 Answer

It looks like you missed a closing curly-brace for your try block if I'm looking at it correctly.