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

Josh Lee
Josh Lee
4,707 Points

What am I doing wrong here? Is there another file i need to look at?

When I try to compile, it goes to StudentCode.cs. How can i look at that file? Any help on this problem would be appreciated.

Thanks!

Program.cs
using System;

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

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

try
{
    int value = 25;
}

catch(Exception)
{
    Console.WriteLine("The Value is out of range.")
}

1 Answer

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

This is the namespace you use

using System;

value variable is declared which you named value, you read the value from the keyboard and convert to integer, assign to variable value

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

if the value variable value is less than zero or greater than 20 system will throw an exception if (value < 0 || value > 20) { throw new System.Exception(); }

we use the try catch blocks to print the exception(find what exception the system throw). 

try { value = 25; }

catch(Exception) { Console.WriteLine("The Value is out of range.") }

Solution: here you check if the variable is smaller than 0 OR  bigger 20 it will throw exception. when it not trigring the condition it will display message.

try { if (value < 0 || value > 20) { throw new System.Exception(); } } catch() { Console.WriteLine("The Value is out of range.") }

For a better explicit answer leave a snapshout of your work book.

#Enjoy Coding