First off, don't use Convert methods to process user input: use TryParse instead:
string input = Console.ReadLine();
if (!double.TryParse(input, out loanAmount))
{
Console.WriteLine("Please enter a double value.\n");
continue;
}
That way, you don't have to mess with an exception, and the code is much clearer to read (particularly when the code gets to a reasonable length).
Second, you can only throw or catch actual Exceptions: classes which are derived from the Exception class, in the same way that displayable forms in windows must derive from teh Form class, which provides then with a border, a title bar, a control box, and many other things, along with a "blank canvas" for you to display your controls.
So change this:
class MyRangeException
{
To this:
class MyRangeException : Exception
{
And see what happens.
And do yourself a favour: stop putting in "redundant comments" - there is no point in adding a comment like this:
}
when the matching open and close brackets (and the indentation which VS updates) tells us what it is anyway!
Comments should be used to explain code: why it's there, how it works - not what the code is!
I'd also suggest that instead of using a variable called
continueLoop
you use
do ... while(true);
and then use
break
to leave.
do
{
try
{
Console.Write("Please enter the loan amount: $");
string input = Console.ReadLine();
if (!double.TryParse(input, out loanAmount))
{
Console.WriteLine("Please enter a double value.\n");
continue;
}
CheckLoanAmount(loanAmount);
break;
}
catch (MyRangeException negativeNumberException)
{
Console.WriteLine( "Please enter a positive value.\n" );
}
} while (true);