Jump to content

Message Box Problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
I'm trying to display a message box when my new exception has been caught but its just showing a generic message from the system (I think) and not my custom message, any ideas of what I'm doing wrong, I had it working but now Its not :(

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;


namespace Exceptions

{

    class ExceptionTest

    {

        static void Main(string[] args)

        {


            bool flag = true;

            while (flag)

            {


                Random randomGenerator = new Random(); //Creates a New Random object


                try

                {

                    Console.Write("Please Enter Your Minimum Number: ");

                    int minNumber = Convert.ToInt32(Console.ReadLine());


                    Console.Write("Please Enter Your Maximum Number: ");

                    int maxNumber = Convert.ToInt32(Console.ReadLine());



                    for (int number = 0; number <= 4; number++)

                    {

                        int randomNumber = randomGenerator.Next(minNumber, maxNumber);

                        Console.WriteLine("{0}", randomNumber);

                    }


                    if (minNumber > maxNumber)

                    {

                        throw new Exception("Your Minimum Number is bigger than your Maximum Number");

                        

                    }


                    flag = false;


                }//End Try Block



                catch (FormatException e)

                {

                    MessageBox.Show(e.Message, "Error Detected", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }


                catch (Exception e)

                {

                    MessageBox.Show(e.Message, "Error Detected", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }



            }



        }



    }

}


#2
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Put your test if min number is greater than max number before the for loop.
if (minNumber > maxNumber)
{
    throw new Exception("Your Minimum Number is bigger than your Maximum number");
}

for (int number = 0; number <= 4; number++)
{
    int randomNumber = randomGenerator.Next(minNumber, maxNumber);
   Console.WriteLine("{0}", randomNumber);
}

Edited by Jaan, 20 November 2009 - 09:07 AM.
Please use code tags when you are posting your codes!


#3
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Thanks works fine now, thought it would have been a simple solution! :thumbup: