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

Aaron Selonke
Aaron Selonke
10,323 Points

"try" Code and "catch" Exceptions in the Frog program

because of the scope of the minutes variable, he moves the entire program into the Try-Catch expression.
Could't he just declare the variable at the beginning on the program?

Andreas Berggren
Andreas Berggren
Courses Plus Student 10,355 Points

My personal preference would be to have a limited try block ( and put the variable in the beginning of the program to have an overview )in order to not have to many things that can go bad in the try block. Also you can Int32.TryParse method to see if it is a number. -> https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

2 Answers

Steven Parker
Steven Parker
230,248 Points

That's also a valid approach. It only requires an extra line of code to declare the variable prior to the try. Then inside the try you can do a simple assignment.

His approach takes into consideration the fact that all of the moved code only makes sense when the Parse succeeds. Instead of thinking of it as being moved, consider it as being left as-is but with a try block wrapped around it.