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

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Moving code into the "try" block (variable scope).

In minute 3:40 lecturer is saying that we need to copy portion of code because scope of declared variable is not reaching that code. But why we have to that, couldn't we just declare variable globally?

Adam McGrade
Adam McGrade
26,333 Points

You want to declare the variable inside of the try block because the code that follows the variable declaration depends on the success of the Parse method. In the video, we are using the Parse method to parse the value of "entry" into a variable of type integer. If this fails, the Parse method will throw an exception that will be caught by the catch part of the try/catch block. This will prevent the application from crashing due to the exception, as we are handling the exception.

If we tried to do this globally, and the value could not be parsed into an integer successfully, then an exception would be thrown and our application would crash.

1 Answer

Chris Braun
Chris Braun
1,606 Points

I had the same question and I don't quite understand Adam's answer. The idea, as far as I am concerned, was to only declare the variable before the try block:

int minutes;

Then define it within the try block, so the exception (if any) would still be caught. Also in this case there does not seem to be a need to include the rest of the code in the try block, so only this definition could be included in the try block, and the rest is outside:

try
{
minutes = int.Parse(entry);
}
catch(FormatException)
{
// ...
}

I have tried running this code and it seems to work properly. So what would be the rationale to include everything in the try block instead of only the definition that may trigger the exception? Thanks for your help.

Adam McGrade
Adam McGrade
26,333 Points

My understanding is that the reason for declaring the minutes variable inside the try block is to avoid declaring unnecessary global variables. As programs grow larger and more complex, it becomes harder to keep track of where a global variables value is changed. If you have some logic that depends on these values, this can result in bugs in the code.

In the video, the minutes variable is only used in one place within the application, so it makes more sense to declare it inside the try block. The minutes variable will then only be accessible within the try block which is fine because this is the only place we need to use it. This protects the minutes variable's value from being changed anywhere else in the application. If we were using minutes throughout the application, it would definitely make sense to use it as you described.

Hope that helps?