256
LESSON 21 Handling Errors
FIGURE 211
Lesson Requirements
Copy the program you built in Lesson 19, Exercise 4 (or download Lesson 19’s version from
the book’s web site).
The previous version of this program uses repeated code to perform calculations when the
user clicks either Calculate or OK. Move that code into a new ValuesAreOk method that
validates the user’s inputs. In addition to protecting the form from format errors, the func-
tion should verify that:
Item name is not blank.
Price Each > 0.
Quantity > 0.
If
ValuesAreOk finds a problem, it should:
Tell the user.
Set focus to the textbox that caused the problem.
Return
false.
If
ValuesAreOk finds that all of the values are okay, it should return true.
Hints
If the user clicks the OK button, the form should close only if the user’s inputs are valid. Be
sure the OK button’s DialogResult property doesn’t automatically close the form.
Use
try-catch blocks to protect against format errors.
Step-by-Step
Copy the program you built in Lesson 19, Exercise 4 (or download Lesson 19’s version from
the book’s web site).
1. This is straightforward.
596906book.indd 256 4/8/10 7:40:27 AM
Try It
257
The previous version of this program uses repeated code to perform calculations when the
user clicks either Calculate or OK. Move that code into a new ValuesAreOk method that
validates the user’s inputs. In addition to protecting the form from format errors, the func-
tion should verify that:
Item name is not blank.
Price Each > 0.
Quantity > 0.
1. Copy the item name into a string variable and verify that its length is at least 1.
2. Parse the Price Each and verify that it is greater than zero.
3. Parse the Quantity and verify that it is greater than zero.
If
ValuesAreOk finds a problem, it should:
Tell the user.
Set focus to the textbox that caused the problem.
Return
false.
1. Whenever the function finds a problem, it should display a message box, set focus to
the textbox containing the error, and return
false. For example, the following code
shows how you might verify the price each:
// Validate price each.
try
{
PriceEach = decimal.Parse(priceEachTextBox.Text, NumberStyles.Any);
if (PriceEach <= 0)
{
MessageBox.Show(“Price Each must be greater than 0.”);
priceEachTextBox.Focus();
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(“Price Each must be a dollar amount. ” +
ex.Message);
priceEachTextBox.Focus();
return false;
}
If
ValuesAreOk finds that all of the values are okay, it should return true.
1. Each of the method’s tests returns if there is a problem so, if the code reaches the end of
the routine, all of the tests passed. At that point, simply return
true.
Please select Lesson 21 on the DVD to view the video that accompanies this lesson.
596906book.indd 257 4/8/10 7:40:28 AM
258
LESSON 21 Handling Errors
EXERCISES
1. Copy the LCM program you built in Lesson 20, Exercise 1 (or download Lesson 20’s version
from the book’s web site) and add error handling to it. If a value causes an error, display a
message and set focus to its textbox. Hints: Validate both the GCD and LCM methods so
they only allow inputs greater than 0. That way they’re both covered if a different program
uses GCD directly. Also use a
try-catch block in the Calculate button’s Click event handler
to protect against format errors.
2. Copy the Fibonacci program you built in Lesson 19, Exercise 2 (or download Lesson 19’s
version from the book’s web site) and add error handling and validation to it. Protect the
program against format errors. Also move the calculation itself into a new method. Make
the method throw an error if its input is less than 0.
3. (SimpleEdit) Copy the SimpleEdit program you built in Lesson 20, Exercise 4 (or download
Lesson 20’s version from the book’s web site) and add error handling to the functions that
open and save files. To test the program, open the file Test.rtf in Microsoft Word. Then make
changes in the SimpleEdit program and try to save them into that file.
4. The quadratic equation finds solutions to equations with the form where a, b, and c are
constants.
a
*
x
2
+ b
*
x + c = 0
The solutions to this equation (the values of x that make it true) are given by the quadratic
formula:
x =
–b ± b
2
– 4ac
2a
Build a program similar to the one shown in Figure 21-2
that calculates solutions to quadratic equations. Use a
try-catch block to protect against format errors. Hints:
Use
Math.Sqrt to take square roots. The equation has
zero, one, or two real solutions depending on whether the
discriminant b
2
– 4ac is less than, equal to, or greater than
zero. Use
if statements to avoid trying to take the square
root of a negative number.
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find them in
the Lesson21 folder.
FIGURE 212
596906book.indd 258 4/8/10 7:40:29 AM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.109.75