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

Jude Brown
PLUS
Jude Brown
Courses Plus Student 629 Points

(63,26) ConsoleWriteLine does not exist in the current context. Help?

I've tried moving the ConsoleWriteLine into the Try block but no dice (at least the way I am doing it.) The ConsoleWriteLine the error is reffering to is the second to last one: ConsoleWriteLine("That is not a valid entry."); continue;

(I think)

Anyone? If I need to paste all of my code please let me know. Thanks in advance.

Jude

try { //Add minutes exercised to total int minutes = int.Parse(userEntry);

                      if(minutes <0)
                      {
                        Console.WriteLine(minutes + " 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 are just showing off.");
                      }


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

                   }
                    catch(FormatException)
                   {
                     ConsoleWriteLine("That is not a valid entry.");
                     continue;
                   }   
                        //Display running total of minutes exercised on screen
                        Console.WriteLine("You have exercised a total of " + runningTotal + " minutes.");

                }
Steven Parker
Steven Parker
229,657 Points

A better way to share code (and the entire environment) is to make a snapshot of your workspace and post the link to it here.

1 Answer

Steven Parker
Steven Parker
229,657 Points

This issue is not related to the "try" block. The mostly likely cause of this error would be a missing "using System;" directive at the top of the file.

Alternately, you could change all instances of "Console.WriteLine" to "System.Console.WriteLine".

Jude Brown
Jude Brown
Courses Plus Student 629 Points

Thank you Steven! I will use snapshot for sure and work on the other suggestions now. Course is great FYI.

Jude Brown
Jude Brown
Courses Plus Student 629 Points

Yup - that worked. commented out the using System; and them added System where needed. I also found a...spelling mistake? Syntax error?

Whatever you call it when you don't spell out your methods correctly.

Cheers,

Jude

Steven Parker
Steven Parker
229,657 Points

The compiler may call it a syntax error. I'd call it a spelling error or typo.