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

Variable Scope Code Challenge

Not sure what if Im solving it right. I do keep getting an error about the catch whenever I put anything in there.

Program.cs
using System;

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

                if (input == "quit")
                {
                    string output = "Goodbye.";
                }
                else
                {
                    string output = "You entered " + input + ".";
                }
            }
            catch()
            {
            }
            Console.WriteLine(output);
        }
    }
}

1 Answer

Roman Fincher
Roman Fincher
18,267 Points

There are a few problems:

First, you don't need the "()" after catch, just the word catch by itself

try 
{ 
    // do something
}
catch
{
    // do something (or nothing!)
}

(Note, the parentheses are used if catching something specific like catch (System.Exception) {}

But you'll still have some other problems:

  • You never initialized input (you need: var input = Console.ReadLine())
  • Because of above, when the code got to this line of the try block, it will fail and go to catch
  • You can't use output after the try/catch block, because it won't be defined if try fails (i.e. move Console.WriteLine(output) to the inside of the try block)

Try again with that info. I don't want to give you the full answer if that will get you what you need. Comment back if you need any more help!