Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have managed to figure out the issue with my previous question but am still struggling with the exceptions
I currently have the main file as:

using System;

namespace Assignment_11
{
    class Program : MyRangeException
    {
        public static double loanAmount;
        public static double years;
        public static double interest;
        static void Main(string[] args)
        {
            double LoanA;
            double Years;
            double IntR;
            double Int = 0;
            bool continueLoop = true;

            //for loanAmount
            do
            {

                try
                {
                    Console.Write("Please enter the loan amount: $");
                    loanAmount = Convert.ToDouble(Console.ReadLine());
                    CheckLoanAmount(loanAmount);
                    continueLoop = false;
                }
                catch (FormatException formatException)
                {
                    Console.WriteLine("\n" + formatException.Message);
                    Console.WriteLine("Please enter a double value.\n");
                } // end catch
                catch (MyRangeException negativeNumberException)
                {
                    Console.WriteLine("\n" + negativeNumberException.Message);
                    //Console.WriteLine( "Please enter a non-negative value.\n" );
                } // end catch
            } while (continueLoop);

         //for years
         do
            {
                try
                {
                    Console.Write("Please enter the number of years for the loan: ");
                    years = Convert.ToDouble(Console.ReadLine());
                    CheckLoanYears(years);
                    continueLoop = false;
                }
                catch (FormatException formatException)
                {
                    Console.WriteLine("\n" + formatException.Message);
                    Console.WriteLine("Please enter a double value.\n");
                } // end catch
                catch (MyRangeException negativeNumberException)
                {
                    Console.WriteLine("\n" + negativeNumberException.Message);
                    //Console.WriteLine( "Please enter a non-negative value.\n" );
                } // end catch

            } while (continueLoop);

         //for interest
         do
            {
                try
                {
                    Console.Write("Please enter the interest rate for the loan: ");
                    interest = Convert.ToDouble(Console.ReadLine());
                    CheckLoanInterest(interest);
                    continueLoop = false;
                }
                catch (FormatException formatException)
                {
                    Console.WriteLine("\n" + formatException.Message);
                    Console.WriteLine("Please enter a double value.\n");
                } // end catch
                catch (MyRangeException negativeNumberException)
                {
                    Console.WriteLine("\n" + negativeNumberException.Message);
                    //Console.WriteLine( "Please enter a non-negative value.\n" );
                } // end catch

            } while (continueLoop);

            C1 instance = new(LoanA, Years, Int, IntR);

            instance.PayInterests();
            instance.iMessage();

        }
    }

In the range exception class I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_11
{
    class MyRangeException
    {
        public MyRangeException(string message);
        public static void CheckLoanAmount(double la)
        {
            if (la < 50000)
                throw new MyRangeException("Loan Amount must be $50,000 or more.");
        }
        public static void CheckLoanInterest(double it)
        {
            if (it < 1)
                throw new MyRangeException("Interest Rate must be 1% or more.");
        }
        public static void CheckLoanYears(double yr)
        {
            if (yr < 5)
                throw new MyRangeException("Years must be 5 years or more.");
        }

    }
}
I have two more files but there are no error messages in those so I feel they are unnecessary but if needed I can put them in after.

What I have tried:

I have tried looking for tutorials on exceptions in this context but it is hard to understand in the context of this code.
Posted
Updated 15-Apr-21 20:09pm
Comments
BillWoodruff 16-Apr-21 0:12am    
Post the exceptions, and, describe exactly where they occur.
[no name] 16-Apr-21 0:19am    
Exceptions inherit from System.Exception; yours doesn't

1 solution

First off, don't use Convert methods to process user input: use TryParse instead:
C#
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:
C#
class MyRangeException
{

To this:
C#
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:
C#
} // end catch
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.
C#
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);
 
Share this answer
 
Comments
BillWoodruff 17-Apr-21 12:57pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900