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# Objects Inheritance Throwing Exceptions

how to throw System.Exception?

Isn't this the way I can catch an Exception and throw the System.Exception?

Program.cs
int value = int.Parse(Console.ReadLine());

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

try
{
    value < 0;
}
catch(Exception)
{
    Console.WriteLine(System.Exception);
}

2 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

try/catch is only for running running code that might throw an exception.

In this challenge you're not asked to use a try/catch, but you're asked to check something and throw an exception if the condition is false..

Sometimes it can be helpful to write comments about the challenge like this:

// if the value integer is less than 0 or greater than 20
  // throw an exception

Geeez Henrik you're the best, I've just made it ;)

keep on rocking!

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Catching and throwing exceptions are two different things

catching exception

try
{
    // something
}
catch (System.Exception)  // (System.Exception e) if you need the variable
{
    // do something
}

throwing an exception

throw new System.Exception();

Thanks Henrik

I tried throw the System.Exception, but still didn't make it

try { value < 0; } catch () { throw new System.Exception(); }