I have tried multiple ways for my program to catch an error by using try get for when the user does not enter any data into the text box for one of the text boxes and for the other when the user does not enter exactly 9 numbers into a text block. I am using C# WPF.
I have tried lots of different methods. one that seemed to work is when i converted to an integer, it seemed to catch it for some reason but i am using strings instead. For example
try
{ // remmeber, textboxes always capture the data as a string therefore we need to convert to an integer CourseDetails.Name = Convert.ToInt32(txtName.Text); CourseDetails.SCNnumber = Convert.ToInt32(txtSCNnumber.Text);
}
// if something does go wrong with any of the instructions in the try block then we will catch the error rather than crash the program
catch (Exception)
{ MessageBox.Show("Please complete all fields"); return;
}
try
{ if (txtName.Text.Length < 0) { MessageBox.Show("Enter your full name"); } else { CourseDetails.Name = txtName.Text; } if (txtSCNnumber.Text.Length != 9) { MessageBox.Show("SCN number must be 9 characters long"); } else { CourseDetails.SCNnumber = txtSCNnumber.Text; }
}
catch (Exception)
{ MessageBox.Show("Please complete all fields");
}The result I'm looking for is when a user inputs data into the first text box for their name it should save to the variable CourseDetails.Name otherwise if they leave it blank the program will catch this as an error an display a message.
for the second text box if the user enters anything other than 9 characters then the program will display an error message stating that the phone number must be more than 9 characters. otherwise the program will save the users input into the variable CourseDetails.SCNnumber
3 Answers
A try-catch block catches exceptions. To catch an exception, an exception has to be thrown. Your first try-catch block will work, because Convert.ToInt32 will throw an FormatException if the input is invalid as documented here.
To make the second try-catch block work, you have to throw an exception on invalid input.
try
{ if (txtName.Text.Length < 0) { throw new ValidationException("Please enter user name") } // ...
}
catch(ValidationException ex)
{ MessageBox.Show(ex.Message);
}As you see i catch on a specific exception type. Is it generally bad practise to catch on the Exception type as you may catch exceptions you can't handle proper inside that catch block. Swallowing those can increase the debigging difficulty significantly.
I would also note Exceptions are not the perfect way to perform more complex validation logic, since a throw jumps right to the next matching catch, so not all fields will be validated.
You have to understand what Try-Catch blocks are used for. Their primary role is to handle exceptions in your program. These exceptions can be compiler exceptions which is thrown by the CLR or program code if there is an error in the program. These exceptions need to be handled to prevent crashing of program. C# provides built-in support to handle the exception using try, catch & finally block.
Now in your code, don't show your MessageBox.Show in your Exception block. What this basically means is that only when an exception is thrown, then your MessageBox will display. This exception (in reference to your code) would be if there is an incorrect conversion of Integer of your txtName.Text.
Instead use If-Else conditions in your scenario. For example:
//Try to parse your captured data to Integer
try
{ if(txtName.Text == "" && txtSCNnumber.Text == "") { MessageBox.Show("Please complete all fields"); } else { // remmeber, textboxes always capture the data as a string therefore we need to convert to an integer CourseDetails.Name = Convert.ToInt32(txtName.Text); CourseDetails.SCNnumber = Convert.ToInt32(txtSCNnumber.Text); }
}
//If the parse fails, then throw an exception
catch (Exception ex)
{ MessageBox.Show(ex.Message);
} Thanks for your input guys. I decided to leave the Try-Catch out after reading your comments I realised it wasn't fit for what I was trying to do. I kept it simple with If-Else statements as shown below.
if (txtName.Text == "" && txtSCNnumber.Text == "") { MessageBox.Show("Please complete all fields"); txtName.Focus(); } else if (txtSCNnumber.Text.Length != 9) { MessageBox.Show("You have entered an invalid SCN number"); txtSCNnumber.Focus(); } else { CourseDetails.Name = txtName.Text; CourseDetails.SCNnumber = txtSCNnumber.Text; } 0