I have to make a guessing game program using CLR Windows Form APP in Visual Studio 2008. The program has to catch excceptions. Im trying to catch an exception if I were to leave the input box called guessBox2 in the program blank and hit the submit button. IM STUCK. here is my code

#pragma once
#include <time.h>
#include<stdlib.h>



namespace Blah 
{
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		int x;
		
		void get_number()
			{
			srand(time(0));//initalizes random numbers
			x = rand() % 1001;//random number 1-1000
			}
		Form1(void)
		{
			InitializeComponent();
			get_number();
			//
			//TODO: Add the constructor code here
			//
			

		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  guessButton;
	protected: 
	private: System::Windows::Forms::TextBox^  guessBox1;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  guessBox2;
	private: System::Windows::Forms::Button^  Reset;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->guessButton = (gcnew System::Windows::Forms::Button());
			this->guessBox1 = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->guessBox2 = (gcnew System::Windows::Forms::TextBox());
			this->Reset = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// guessButton
			// 
			this->guessButton->Location = System::Drawing::Point(82, 174);
			this->guessButton->Name = L"guessButton";
			this->guessButton->Size = System::Drawing::Size(142, 78);
			this->guessButton->TabIndex = 0;
			this->guessButton->Text = L"Guess";
			this->guessButton->UseVisualStyleBackColor = true;
			this->guessButton->Click += gcnew System::EventHandler(this, &Form1::guessButton_Click);
			// 
			// guessBox1
			// 
			this->guessBox1->Location = System::Drawing::Point(107, 127);
			this->guessBox1->Name = L"guessBox1";
			this->guessBox1->Size = System::Drawing::Size(105, 20);
			this->guessBox1->TabIndex = 1;
			this->guessBox1->Text = L"Enter Number Below";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(12, 62);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(323, 13);
			this->label1->TabIndex = 2;
			this->label1->Text = L"I have a number between 1 and 1000- can you guess my number\? ";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(193, 99);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(142, 13);
			this->label2->TabIndex = 3;
			this->label2->Text = L"Please enter your first guess ";
			// 
			// guessBox2
			// 
			this->guessBox2->Location = System::Drawing::Point(107, 147);
			this->guessBox2->Name = L"guessBox2";
			this->guessBox2->Size = System::Drawing::Size(105, 20);
			this->guessBox2->TabIndex = 4;
			// 
			// Reset
			// 
			this->Reset->Location = System::Drawing::Point(1, 174);
			this->Reset->Name = L"Reset";
			this->Reset->Size = System::Drawing::Size(75, 78);
			this->Reset->TabIndex = 5;
			this->Reset->Text = L"Play Again";
			this->Reset->UseVisualStyleBackColor = true;
			this->Reset->Click += gcnew System::EventHandler(this, &Form1::Reset_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));
			this->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Center;
			this->ClientSize = System::Drawing::Size(549, 325);
			this->Controls->Add(this->guessBox1);
			this->Controls->Add(this->Reset);
			this->Controls->Add(this->guessBox2);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->guessButton);
			this->Cursor = System::Windows::Forms::Cursors::Default;
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
			this->Name = L"Form1";
			this->Text = L"Guess from 1-1000";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
private: System::Void guessButton_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				 
				 int y=Convert::ToInt32(guessBox2->Text);
				 if(y<x && !(y<x-2))
					 label1->Text="Warmer";
				 if (y>x && !(y>x+2))
					 label1->Text="Colder";
				 if(y<x)
				 {
					 this->label2->Text="Cold, Less";
				 guessBox2->BackColor=System::Drawing::Color::Blue;
				 }
				 if(y>x)
				 {
					label2->Text="Greater Than number";
				 guessBox2->BackColor=System::Drawing::Color::Red;
				 }
				 if(y==x)
				 {
					 this->guessBox1->Text=
					 Convert::ToString(x);
					 guessBox1->BackColor=System::Drawing::Color::White;
					 label2->Text="CORRECT!!!";
				 guessBox2->BackColor=System::Drawing::Color::Green;
				 guessBox2->ReadOnly=true;
				 }
				 guessBox2->BackColor=System::Drawing::Color::Azure;
				 }
		 }
private: System::Void Reset_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 this->get_number();
			 guessBox2->ReadOnly=false;
			 guessBox2->BackColor=System::Drawing::Color::White;
		 }

};
}

I have nearly no knowledge of this .NET stuff but maybe Convert::ToInt32() throws an exception if provided with an illegal input string. Otherwise you might examine the return value of Convert::ToInt32() and throw an exception if it signals wrong input.

I'm gueessing this line will throw an exception if it is not a digit

int y=Convert::ToInt32(guessBox2->Text);

so but this code in a try block and have a corresponding catch block to handle the exception

This article has been dead for over six months. Start a new discussion instead.