csharp icon indicating copy to clipboard operation
csharp copied to clipboard

user-defined-exceptions - refactor code to illustrate concept more clearly

Open mikedamay opened this issue 4 years ago • 0 comments

The user-defined-exception exercise has a story in which we are using a test harness to test a calculator by calling its Multiply() method. See this example.

We are attempting to show two concepts: user-defined-exceptions/wrapping an exception and exception-filtering.

There are currently 2 methods in the CalculatorTestHarness class TestMultiplication and Multiply) which do similar things (one calls the other) as well as a method also called Multiply() in the Calculator class. The existence of 3 such similar sounding routines is confusing so refactoring is in order.

If we remove the Multiply() method from CalculatorTestHarness and move its try-catch block into the Calculator.Multiply() method (putting the checked multiplication into the try block then it will make more sense. e.g.:

public class Calculator
{
    public int Multiply(int x, int y)
    {
        try
        {
            checked
            {
                return x * y;
            }
        }
        catch (OverflowException ofex)
        {
            // TODO: implement catch block
        }

        return 0;    // TODO: remove this statement when implemented the catch logic
    } 
}

Changes will be required to:

  • instructions.md
  • hint.md
  • Example.cs
  • UserDefinedExceptions.cs
  • UserDefinedExceptionsTest.cs

mikedamay avatar Jun 17 '20 12:06 mikedamay