AS 2019 DisplayErrorMessages

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Issue

The skeleton program has some built in error handling, some is based around exception handling and others are determined by the in-game logic. The program just displays an error code and no other information. The following errors are generated:

  • The SetupBoard method has a try & catch block, this runs DisplayErrorCode(4) , this will be an error when loading the file.
  • The SelectMove method has a try & catch block, this runs DisplayErrorCode(3) (approx line 440), this will be an error cause by entering a non integer value for the Row or Column.
  • The SelectMove method also runs DisplayErrorCode(1) (approx line 399), this be displayed because the piece doesn't exit.
  • The SelectMove method also runs DisplayErrorCode(2) (approx line 428), this will be displayed when the piece you are trying to move can't move. ie it is not in the list of possible moves.

Where

The method called is:

        private static void DisplayErrorCode(int errorNumber)
        {
            Console.WriteLine("Error " + errorNumber);
        }

Idea

You should add a Switch..Case statement after the current Console.WriteLine to also display a message to explain the error. So you could:

        private static void DisplayErrorCode(int errorNumber)
        {
            Console.WriteLine("Error " + errorNumber);
            string message = "";
            switch(errorNumber)
            {
                case 1:
                    message = "Piece does not exist";
                    break;
                case 2:
                    message = "Piece can't move";
                    break;
                case 3:
                    message = "Invalid position";
                    break;
                case 4:
                    message = "File error";
                    break;
            }
            Console.WriteLine(message);
        }