c# tutorial msm_murach chapter-07-slides
TRANSCRIPT
![Page 1: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/1.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 1
Chapter 7
How to handle exceptions and validate data
![Page 2: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/2.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 2
Objectives
Applied 1. Given a form that uses text boxes to accept data from the user,
write code that catches any exceptions that might occur. 2. Given a form that uses text boxes to accept data and the
validation specifications for that data, write code that validates the user entries.
3. Use dialog boxes as needed within your applications.
![Page 3: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/3.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 3
Objectives (continued)
Knowledge 1. Describe the Exception hierarchy and name two of its subclasses. 2. Describe the use of try-catch statements to catch specific
exceptions as well as all exceptions. 3. Describe the use of the properties and methods of an exception
object. 4. Describe the use of throw statements. 5. Describe the three types of data validation that you’re most likely
to perform on a user entry. 6. Describe two ways that you can use generic validation methods in
a method that validates all of the user entries for a form.
![Page 4: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/4.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 4
The dialog box for an unhandled exception
![Page 5: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/5.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 5
The Exception hierarchy for five common exceptions System namespace
Exception
DivideByZeroExceptionOverflowException
ArithmeticExceptionFormatException
![Page 6: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/6.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 6
Methods that might throw exceptions Class Method Exception Convert ToDecimal(string) FormatException Convert ToInt32(string) FormatException Decimal Parse(string) FormatException DateTime Parse(string) FormatException
![Page 7: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/7.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 7
The syntax to display a dialog box with an OK button
MessageBox.Show(text[, caption]);
A dialog box with an OK button
The statement that displays this dialog box MessageBox.Show( "Please enter a valid number for the Subtotal field.", "Entry Error");
![Page 8: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/8.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 8
The syntax for a simple try-catch statement try { statements } catch { statements }
A try-catch statement try { decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); decimal discountPercent = .2m; decimal discountAmount = subtotal * discountPercent; decimal invoiceTotal = subtotal - discountAmount; } catch { MessageBox.Show( "Please enter a valid number for the Subtotal " + "field.", "Entry Error"); }
![Page 9: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/9.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 9
The dialog box that’s displayed if an exception occurs
![Page 10: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/10.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 10
The syntax for a try-catch statement that accesses the exception try { statements } catch(ExceptionClass exceptionName) { statements }
Two common properties for all exceptions Property Description Message Gets a message that briefly describes the current
exception. StackTrace Gets a string that lists the methods that were called
before the exception occurred.
A common method for all exceptions Method Description GetType() Gets the type of the current exception.
![Page 11: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/11.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 11
A try-catch statement that accesses the exception try { decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); } catch(Exception ex) { MessageBox.Show( ex.Message + "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception"); }
![Page 12: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/12.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 12
The dialog box that’s displayed if an exception occurs
![Page 13: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/13.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 13
The complete syntax for the try-catch statement try { statements } catch(MostSpecificException [exceptionName]) { statements }... [catch(NextMostSpecificException [exceptionName]) { statements }]... [catch([LeastSpecificException [exceptionName]]) { statements }] [finally { statements }]
![Page 14: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/14.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 14
A try-catch statement that catches two specific exceptions
try { decimal monthlyInvestment = Convert.ToDecimal(txtMonthlyInvestment.Text); decimal yearlyInterestRate = Convert.ToDecimal(txtInterestRate.Text); int years = Convert.ToInt32(txtYears.Text); } catch(FormatException) // a specific exception { MessageBox.Show( "A format exception has occurred. " + "Please check all entries.", "Entry Error"); }
![Page 15: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/15.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 15
A try-catch statement that catches two specific exceptions (continued)
catch(OverflowException) // another specific exception { MessageBox.Show( "An overflow exception has occurred. " + "Please enter smaller values.", "Entry Error"); } catch(Exception ex) // all other exceptions { MessageBox.Show(ex.Message, ex.GetType().ToString()); } finally // this code runs whether or not // an exception occurs { PerformCleanup(); }
![Page 16: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/16.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 16
The syntax for throwing a new exception throw new ExceptionClass([message]);
The syntax for throwing an existing exception throw exceptionName;
When to throw an exception When a method encounters a situation where it isn’t able to
complete its task. When you want to generate an exception to test an exception
handler. When you want to catch the exception, perform some processing,
and then throw the exception again.
![Page 17: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/17.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 17
A method that throws an exception when an exceptional condition occurs
private decimal CalculateFutureValue( decimal monthlyInvestment, decimal interestRateMonthly, int months) { if (monthlyInvestment <= 0) throw new Exception("Monthly Investment must " + "be greater than 0."); if (interestRateMonthly <= 0) throw new Exception("Interest Rate must be " + "greater than 0."); . . }
![Page 18: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/18.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 18
Code that throws an exception for testing purposes
try { decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); throw new Exception("An unknown exception " + "occurred."); } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception"); }
![Page 19: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/19.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 19
Code that rethrows an exception try { Convert.ToDecimal(txtSubtotal.Text); } catch (FormatException fe) { txtBox.Focus(); throw fe; }
![Page 20: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/20.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 20
The code for the Future Value application with exception handling private void btnCalculate_Click(object sender, System.EventArgs e) { try { decimal monthlyInvestment = Convert.ToDecimal(txtMonthlyInvestment.Text); decimal yearlyInterestRate = Convert.ToDecimal(txtInterestRate.Text); int years = Convert.ToInt32(txtYears.Text); decimal monthlyInterestRate = yearlyInterestRate / 12 / 100; int months = years * 12; decimal futureValue = this.CalculateFutureValue( monthlyInvestment, monthlyInterestRate, months); txtFutureValue.Text = futureValue.ToString("c"); txtMonthlyInvestment.Focus(); }
![Page 21: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/21.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 21
The code for the Future Value application with exception handling (continued) catch(FormatException) { MessageBox.Show( "Invalid numeric format. " + "Please check all entries.", "Entry Error"); } catch(OverflowException) { MessageBox.Show( "Overflow error. Please enter smaller values.", "Entry Error"); } catch(Exception ex) { MessageBox.Show( ex.Message, ex.GetType().ToString()); } }
![Page 22: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/22.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 22
The code for the Future Value application with exception handling (continued) private decimal CalculateFutureValue( decimal monthlyInvestment, decimal monthlyInterestRate, int months) { decimal futureValue = 0m; for (int i = 0; i < months; i++) { futureValue = (futureValue + monthlyInvestment) * (1 + monthlyInterestRate); } return futureValue; }
![Page 23: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/23.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 23
Code that checks that an entry has been made if (txtMonthlyInvestment.Text == "") { MessageBox.Show("Monthly Investment is a required " + "field.", Entry Error); txtMonthlyInvestment.Focus(); }
Code that checks an entry for a valid decimal format try { Convert.ToDecimal(txtMonthlyInvestment.Text); } catch (FormatException) { MessageBox.Show( "Monthly Investment must be a numeric value.", "Entry Error"); txtMonthlyInvestment.Focus(); }
![Page 24: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/24.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 24
Code that checks an entry for a valid range decimal monthlyInvestment = Convert.ToDecimal(txtMonthlyInvestment.Text); if (monthlyInvestment <= 0) { MessageBox.Show( "Monthly Investment must be greater than 0.", "Entry Error"); txtMonthlyInvestment.Focus(); } else if (monthlyInvestment >= 1000) { MessageBox.Show( "Monthly Investment must be less than 1,000.", "Entry Error"); txtMonthlyInvestment.Focus(); }
![Page 25: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/25.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 25
A method that checks for a required field public bool IsPresent(TextBox textBox, string name) { if (textBox.Text == "") { MessageBox.Show(name + " is a required field.", "Entry Error"); textBox.Focus(); return false; } return true; }
![Page 26: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/26.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 26
A method that checks for a valid numeric format public bool IsDecimal(TextBox textBox, string name) { try { Convert.ToDecimal(textBox.Text); return true; } catch(FormatException) { MessageBox.Show(name + " must be a decimal " + "value.", "Entry Error"); textBox.Focus(); return false; } }
![Page 27: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/27.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 27
A method that checks for a valid numeric range public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max) { decimal number = Convert.ToDecimal(textBox.Text); if (number < min || number > max) { MessageBox.Show(name + " must be between " + min.ToString() + " and " + max.ToString() + ".", "Entry Error"); textBox.Focus(); return false; } return true; }
![Page 28: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/28.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 28
Code that uses generic methods to check the validity of one entry if (IsPresent(txtMonthlyInvestment, "Monthly Investment") && IsDecimal(txtMonthlyInvestment, "Monthly Investment") && IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000)) { MessageBox.Show("Monthly Investment is valid.", "Test"); }
![Page 29: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/29.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 29
Code that uses a series of simple if statements to validate multiple entries
public bool IsValidData() { // Validate the Monthly Investment text box if (!IsPresent(txtMonthlyInvestment, "Monthly Investment")) return false; if (!IsDecimal(txtMonthlyInvestment, "Monthly Investment")) return false; if (!IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000)) return false; // Validate the Interest Rate text box if (!IsPresent(txtInterestRate, "Interest Rate")) return false; if (!IsDecimal(txtInterestRate, "Interest Rate")) return false; if (!IsWithinRange(txtInterestRate, "Interest Rate", 1, 20)) return false; return true; }
![Page 30: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/30.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 30
Code that uses compound conditions in a single return statement to validate multiple entries
public bool IsValidData() { return // Validate the Monthly Investment text box IsPresent(txtMonthlyInvestment, "Monthly Investment") && IsDecimal(txtMonthlyInvestment, "Monthly Investment") && IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000) && // Validate the Interest Rate text box IsPresent(txtInterestRate, "Yearly Interest Rate") && IsDecimal(txtInterestRate, "Yearly Interest Rate") && IsWithinRange(txtInterestRate, "Yearly Interest Rate", 1, 20); }
![Page 31: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/31.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 31
The Future Value form with a dialog box for required fields
![Page 32: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/32.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 32
The dialog box for invalid decimals
The dialog box for invalid ranges
![Page 33: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/33.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 33
The dialog box for an unanticipated exception
![Page 34: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/34.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 34
The code for the Future Value application private void btnCalculate_Click(object sender, System.EventArgs e) { try { if (IsValidData()) { decimal monthlyInvestment = Convert.ToDecimal(txtMonthlyInvestment.Text); decimal yearlyInterestRate = Convert.ToDecimal(txtInterestRate.Text); int years = Convert.ToInt32(txtYears.Text); int months = years * 12; decimal monthlyInterestRate = yearlyInterestRate / 12 / 100; decimal futureValue = CalculateFutureValue( monthlyInvestment, monthlyInterestRate, months); txtFutureValue.Text = futureValue.ToString("c"); txtMonthlyInvestment.Focus(); } }
![Page 35: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/35.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 35
The code for the Future Value application (cont.) catch(Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception"); } } public bool IsValidData() { return IsPresent( txtMonthlyInvestment, "Monthly Investment") && IsDecimal( txtMonthlyInvestment, "Monthly Investment") && IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000) &&
![Page 36: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/36.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 36
The code for the Future Value application (cont.) IsPresent(txtInterestRate, "Yearly Interest Rate") && IsDecimal(txtInterestRate, "Yearly Interest Rate") && IsWithinRange(txtInterestRate, "Yearly Interest Rate", 1, 20) && IsPresent(txtYears, "Number of Years") && IsInt32(txtYears, "Number of Years") && IsWithinRange(txtYears, "Number of Years", 1, 40); } public bool IsPresent(TextBox textBox, string name) { if (textBox.Text == "") { MessageBox.Show(name + " is a required field.", "Entry Error"); textBox.Focus(); return false; } return true; }
![Page 37: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/37.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 37
The code for the Future Value application (cont.) public bool IsDecimal(TextBox textBox, string name) { try { Convert.ToDecimal(textBox.Text); return true; } catch(FormatException) { MessageBox.Show(name + " must be a decimal value.", "Entry Error"); textBox.Focus(); return false; } }
![Page 38: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/38.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 38
The code for the Future Value application (cont.) public bool IsInt32(TextBox textBox, string name) { try { Convert.ToInt32(textBox.Text); return true; } catch(FormatException) { MessageBox.Show(name + " must be an integer.", "Entry Error"); textBox.Focus(); return false; } }
![Page 39: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/39.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 39
The code for the Future Value application (cont.) public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max) { decimal number = Convert.ToDecimal(textBox.Text); if (number < min || number > max) { MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error"); textBox.Focus(); return false; } return true; }
![Page 40: C# Tutorial MSM_Murach chapter-07-slides](https://reader035.vdocuments.net/reader035/viewer/2022070518/58e60ab71a28ab44778b543d/html5/thumbnails/40.jpg)
Murach’s C# 2010, C7 © 2010, Mike Murach & Associates, Inc. Slide 40
The code for the Future Value application (cont.) private decimal CalculateFutureValue( decimal monthlyInvestment, decimal monthlyInterestRate, int months) { decimal futureValue = 0m; for (int i = 0; i < months; i++) { futureValue = (futureValue + monthlyInvestment) * (1 + monthlyInterestRate); } return futureValue; }