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 trialTiago Ramos
2,380 Pointshow to throw System.Exception?
Isn't this the way I can catch an Exception and throw the System.Exception?
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
Python Web Development Techdegree Student 38,322 Pointstry/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
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsCatching 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();
Tiago Ramos
2,380 PointsThanks Henrik
I tried throw the System.Exception, but still didn't make it
try { value < 0; } catch () { throw new System.Exception(); }
Tiago Ramos
2,380 PointsTiago Ramos
2,380 PointsGeeez Henrik you're the best, I've just made it ;)
keep on rocking!