Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,624 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: