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

Joel Muro
Joel Muro
2,884 Points

try code and catch

using System;

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

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

        Console.WriteLine(output);
    }
}

}

fix the code so that it compiles but dont change the intent of the code. this is the question but i have no idea where to start.

5 Answers

I had to change the code a little to get this one to pass:

static void Main()
        {            
            string input = Console.ReadLine();
            string output = "";

            try{
                if (input == "quit")
                {
                    output = "Goodbye.";
                }
                else
                {
                    output = "You entered " + input + ".";
                }
            }
            catch(Exception ex)
            {
            }

            Console.WriteLine(output);
        }
Joel Muro
Joel Muro
2,884 Points

yeah man I'm still not getting it.

Joel Muro
Joel Muro
2,884 Points

can someone explain using the code what is the issue . can someone go more into detail I don't mind the answer but I want to be able to understand it too.

A try/catch block is something you can use so that if your code throws an exception it wont crash the program. In the video it talks about int.Parse(). If you do not have a try/catch around int.parse and you pass a string that will not parse into an integer, it will throw an exception which your program is not looking to catch. When you wrap executing code in a try/catch, and an exception is thrown, you can choose what to do when the program catches it.

int.Parse("abc"); //program will break
try {
  int.Parse("abc"); //this will throw an exception
}
catch(Exception ex) //this is where the exception is caught and you can do something about it
{
  //do something with exception
}

essentially try/catch helps your program fail gracefully

"essentially try/catch helps your program fail gracefully"

haha

I noticed that he didn't declare what kind of variable he was using when he wrote 'input'. It should be a string.

jason phillips wrote it in his code.

Njemile Siwatu
Njemile Siwatu
1,044 Points

Ahhh Thank you Hugo. It was hard trying to figure out what the exception would be and where to put it.