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 Variable Scope

Andrada Terenche
Andrada Terenche
1,103 Points

Cannot solve try/catch Code Challenge.

As I said in the title, I cannot solve this challenge. I still get the errors that the input and output don't exist in the context. Maybe I didn't exactly understand the request. Also, although I don't have any errors at the Final challenge, it cannot submit it and says to try again (when I do it still doesn't work, but tells me Treehouse will get back to me).

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            input = Console.ReadLine();

            try
            {
            if (input == "quit")
            {
                string output = "Goodbye.";
            }
            else
            {
                string output = "You entered " + input + ".";
            }
            }
            catch (FormatException)
            {
                Console.WriteLine("That is not valid input.");
                continue;
            }

            Console.WriteLine(output);
        }
    }
}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Andrada.

The main problem you are having is this in not a try / catch challenge. The Challenge does not ask you to create a try/catch block. In fact, it explicitly says:

Be sure to not change the intent or intended behavior of the code.

And by adding the try/catch block, you changed the intended behavior.

All the challenge wants is for you to change the scoping of the variables so they are in scope.

Antonio De Rose give a very detailed explanation of this challenge here in this previous post.

Give that look, and it should clear it right up.

Keep coding! :) :dizzy: