Assistance with exceptions. Try to throw an exception class I created, but not sure how…I have the class written, but I’m not sure how to call it when an exception is generated.

My code is below. You'll notice that in my "catch" statement I'm just using a console.writeline statement, but I need to figure out how to call my "MyException" class and throw my error. I'm a bit stumped here.. If anyone could lead me in the right direction I'd appreciate it!

public class MyException : Exception { // String property (get used by test routine) (set used by constructor) public string TheMessage { get; private set; } // Two public data members of type TwoVals // Class TheNumerator and TheDenominator will be filled by the constructor public TwoVals TheNumerator { get; set; } public TwoVals TheDenominator { get; set; } // Constructor that receives a string that can be logged by a catcher of an instance of this class // a TwoVals object called N and a TwoVals object called D public MyException(string message, TwoVals N , TwoVals D) { // Assign the properties to each paramater TheMessage = message; TheNumerator = N; TheDenominator = D; } } public class TwoVals { // Two public properties, X and Y with a public get and private set method. public int x { get; private set; } public int y { get; private set; } // Constructor that receives two values as parameters and assigns them to X and Y public TwoVals(int a, int b) { x = a; y = b; } // Public override method to return X and Y seperated by a space public override string ToString() { string a = x.ToString(); string b = y.ToString(); return a + b; } // Overload for the addition operator that returns the sum of both x and y properties public static TwoVals operator +(TwoVals a, TwoVals b) { return new TwoVals(a.x + b.x, a.y + b.y); } // Overload for the subtraction operator that returns the difference between each x and y property public static TwoVals operator -(TwoVals a, TwoVals b) { return new TwoVals(a.x - b.x, a.y - b.y); } // Overload for the product operator that returns the product of each x and y property public static TwoVals operator *(TwoVals a, TwoVals b) { return new TwoVals(a.x * b.x, a.y * b.y); } // Overload for the division operator that returns the quotient of each x and y property public static TwoVals operator /(TwoVals a, TwoVals b) { return new TwoVals(a.x / b.x, a.y / b.y); } } class Program { static void Main(string[] args) { // Run a loop that will cycle through each case within the switch statement for (int i = 1; i < 4; i++) { // For each int go to the specify case and attempt to perform the logic try { // Show text that loop has begun Console.WriteLine("Loop {0} is starting...", i); switch (i) { case 1: TwoVals Result = (new TwoVals(5, 7) + new TwoVals(0, 1)) / (new TwoVals(10, 20) * new TwoVals(3, 1) - new TwoVals(29, 18)); Console.WriteLine("Equation " + i + " completed with result: " + Result); break; case 2: TwoVals Result1 = (new TwoVals(5, 7) + new TwoVals(-2, -5)) / (new TwoVals(10, 20) * new TwoVals(3, 1) - new TwoVals(29, 20)); Console.WriteLine("Equation " + i + " completed with result: " + Result1); break; case 3: TwoVals Result2 = new TwoVals(1, 5) / (new TwoVals(29, 18) - (new TwoVals(10, 20) * new TwoVals(3, 1))); Console.WriteLine("Equation " + i + " completed with result: " + Result2); break; } // Show text that loop has ended Console.WriteLine("Loop {0} is done...", i); } // Catch DivideByZero exception and output the // numerator object and the denominator object in the exception object recieved catch (DivideByZeroException) { Console.WriteLine("\tDivide By Zero Exception! Keep going..."); } } // Keep program from closing Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(); } } 

}

by atomiq91 via /r/csharp

Leave a Reply