unhandled exception has occurred..

  • Thread starter Thread starter Allen
  • Start date Start date
A

Allen

I use V C++ 2008. From the code below I am trying to make a program that
will ask the user to enter a number into the text box. If the user click
"Enter", without entering a number a dialog box will come up to show that
unhandled exception has occurred..

I want to prevent the unhandled.. dialog box from coming up. I want to use
a message box instead, to promt the user to enter a number and the program
should go on.

I noticed that if I iliminte all codes after the end of the catch block,
only my message box will show up, but the unhandled exption dialog box
would not show up. If I leave the mentioned codes in place, then the
unhanled exception dialog box together wilth my message box will show up.
Can anybody tells me why that is happening and what should I do to fix this
problem.

Thanks a lot.


private: System::Void BtnIn_Click(System::Object^ sender,
System::EventArgs^ e)
{

try

{
empNumBox->Text;
if (empNumBox->Text == "")

throw gcnew System::ArgumentException(L" You have to enter your employee
code");

}

catch(System::ArgumentException^ pex)
{
MessageBox::Show(pex->Message,L" Error ");
}


Int32 temp = Convert::ToInt32(empNumBox->Text);
switch (temp)
{
case 1:
{
Do something
}
break;

default: MessageBox::Show(" This is an invalid employee number.\n Please try
again! ",L" Error ");
}
}
 
And the exception you get is raised from ? You show a message if the
textbox is empty but then your code keeps running and you'll try to convert
this empty textbox to an int. I believe this is where you have another
exception.

I wouldn't use exceptions at all. This is not that something goes wrong in
the code but just that the data entered user is not valid. A VC group might
be better as this is rather a code organization issue rather than a .NET
Framework issue...

My approach would be to validate the user input and if it is wrong to
display an error message and then quit the routine letting the user to
correct before proceeding again...
 
Back
Top