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#

Challenge question about exception in c# beginner tutorial

Hey,

In the challenge about exception, i found some misleading code. The question says: Here's how I wrote the code. Now wrap the testing logic with a try/catch and write the message "Value is out of bounds!" to the console if the exception is caught.

This is the associated code: int value = int.Parse(Console.ReadLine());

if (value < 0 || value > 20) { throw new System.Exception(); }

Console.WriteLine(string.Format("You entered {0}",value));

Now i find it weird that in this code we use: Console.ReadLine() and Console.WriteLine()

But when throwing the exception we still do new System.Exception() and not just new Exception().

Isn't console also using the same System namespace as Exception?

Thank you for your time

2 Answers

Steven Parker
Steven Parker
231,271 Points

If you are "using" the System namespace, that does seem like it should work. Did you try it and have a problem?

It doesn't give me any problem. I just was confused by it. Because Console.WriteLine() is not written as System.Console.WriteLine(), while the throwing of the exception is written as System.Exception(). so i thought maybe these 2 System namespaces are not equal or so? It was just a bit confusing