1
Reply

How to handel error

Henry Lhteenmaki

Henry Lhteenmaki

Dec 21 2011 1:41 PM
1.3k
Hi !

in this particular program I want to stop program for executing the rest of instruction until the error detected by error handler is solved, I try to use return key work , but it did not work for this purpose .
I enter a string value instead a integer , and instead the program force me the correct the input error it ask me to enter the second score even the first one was not correct .

Br

Henry


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment3
{
    class Program
    {
        static void Main(string[] args)
        {
            string name, message = " ";
            int average = 0 ;

            //Users names

            Console.WriteLine("Enter you name");
            name = Console.ReadLine();
           
            // Declare variables the type array

            string[] var = { "first", "second", "third" };
            int[] scores = new int[3];
           

            // loop trought the arrays


            for (int i = 0; i < var.Length; i++)
     
                 try
               
                {
                    Console.WriteLine("Enter your {0}, score", var[i]);
                    scores[i] = int.Parse(Console.ReadLine());
                    average += scores[i]/3;
                }

                catch (FormatException) {

                    Console.WriteLine("the {0}, is Not a numeric value ", scores[i]);

                }

              
            }
          


            // Testing the average

            if (average >= 90 && average <= 100)
            {
           
                message = "Wow, you did great";
            }

            else if (average >= 80 && average <= 89)
            {
           
                 message = "Pretty good!";
            }

            else if (average >= 75 && average <= 79)
            {
           
                message = "That was close....";
           
            }

            else if (average < 75)
            {

                message = "Better luck next time";
           
            }


          
           
            Console.WriteLine("Hi {0}, {1}", name, message);
            Console.ReadLine();
            return;
            }
        }
    }


Answers (1)