How to handle exceptions and validate data

10/5/2008 Objectives Applied Chapter 7 • Given a form that uses text boxes to accept data from the user, write code that catches any exceptions tha...
Author: Chester Elliott
16 downloads 0 Views 144KB Size
10/5/2008

Objectives Applied

Chapter 7

• Given a form that uses text boxes to accept data from the user, write code that catches any exceptions that might occur. • Given a form that uses text boxes to accept data and the validation specifications for that data, write code that validates the user entries.

How to handle exceptions and validate data

Knowledge • Describe the use and limitations of the IsNumeric function for data validation. • Describe the Exception hierarchy and name two of its subclasses. • Describe the use of Try…Catch statements to catch specific exceptions as well as all exceptions.

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 1

Objectives (continued)

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 2

The syntax of the IsNumeric function

• Describe the use of the properties and methods of an exception object.

IsNumeric(expression)

An IsNumeric function used for data validation

• Describe the use of Throw statements. • Describe the three types of data validation that you’re most likely to perform on a user entry. • Describe two ways that you can use generic validation methods in a method that validates all of the user entries for a form. • Describe the use the Validating event and the use of masked text boxes for data validation.

Private Sub btnCalculate_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnCalculate.Click If Not IsNumeric(txtSubtotal.Text) Then MessageBox.Show( Sh ( _ "Please enter a valid number for the " _ & "Subtotal field.", "Entry Error") Exit Sub End If ' The code that does the processing if the entry is ' valid End Sub

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 3

The syntax for displaying a dialog box with an OK button MessageBox.Show(text[, caption])

A statement that displays a dialog box MessageBox.Show( _ "Please enter a valid number for the Subtotal field.", _ "Entry Error")

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 4

Another statement that displays a dialog box MessageBox.Show( _ "An exception has occurred. " & ControlChars.CrLf _ & "The program will be cancelled.", _ "Program Error")

The dialog box for the statement above

The dialog box for the statement above

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 5

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 6

1

10/5/2008

The dialog box for an unhandled exception

Constants that you can use for new lines and tabs Description Carriage return character Carriage return character Tab character

Murach’s Visual Basic 2008, C7

ControlChars member ControlChars.CrLf ControlChars.NewLine ControlChars.Tab

VB constant vbCrLf vbNewLine vbTab

© 2008, Mike Murach & Associates, Inc.

Slide 7

Slide 8

Try trystatements Catch catchstatements End Try

System namespace Exception

ArithmeticException

© 2008, Mike Murach & Associates, Inc.

The syntax for a simple Try...Catch statement

The Exception hierarchy for five common exceptions

FormatException

Murach’s Visual Basic 2008, C7

A Try...Catch statement

InvalidCastException

Try O OverflowException fl E ti

Murach’s Visual Basic 2008, C7

subtotal = CDec(txtSubtotal.Text) di discountPercent tP t = .2D 2D discountAmount = subtotal * discountPercent invoiceTotal = subtotal - discountAmount Catch MessageBox.Show( "Please enter a valid number for the " _ & "Subtotal field.", "Entry Error") End Try

Di id B Z DivideByZeroException E ti

© 2008, Mike Murach & Associates, Inc.

Slide 9

The dialog box that’s displayed if an exception occurs

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 10

The syntax for a Try...Catch statement that accesses the exception Try trystatements Catch exceptionName As exceptionclass catchstatements End Try

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 11

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 12

2

10/5/2008

Two common properties for all exceptions Property Message StackTrace

A Try...Catch statement that accesses the exception

Description Gets a message that briefly describes the current exception. Gets a string that lists the procedures that were called before the exception occurred.

Dim subtotal As Decimal Try subtotal = CDec(txtSubtotal.Text) Catch ex As Exception MessageBox.Show( _ ex.Message & vbCrLf & vbCrLf & ex.StackTrace, _ ex.GetType.ToString) yp g End Try

A common method for all exceptions Method GetType()

Murach’s Visual Basic 2008, C7

Description Gets the type of the current exception.

© 2008, Mike Murach & Associates, Inc.

Slide 13

© 2008, Mike Murach & Associates, Inc.

Slide 15

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 16

A Try...Catch statement that catches specific exceptions (continued) Finally ' this code runs whether or not an exception occurs PerformCleanup() End Try

Try monthlyInvestment = _ Convert.ToDecimal(txtMonthlyInvestment.Text) yearlyInterestRate = _ Convert.ToDecimal(txtInterestRate.Text) years = Convert.ToInt32(txtYears.Text) Catch ex As FormatException ' a specific exception MessageBox.Show( _ "A A format exception has occurred occurred. " _ & "Please check all entries.", _ "Entry Error") Catch ex As OverflowException ' a specific exception MessageBox.Show( _ "An overflow exception has occurred. " _ & "Please enter smaller values.", _ "Entry Error") Catch ex As Exception ' all other exceptions MessageBox.Show(ex.Message, ex.GetType.ToString)

© 2008, Mike Murach & Associates, Inc.

Slide 14

Try trystatements Catch exceptionName As mostspecificexception catchstatements [Catch exceptionName As nextmostspecificexception] Catchstatements] ... Catch exceptionName As leastspecificexception catchstatements [Finally statements] t t t ] End Try

A Try...Catch statement that catches specific exceptions

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

The complete syntax for the Try...Catch statement

The dialog box that’s displayed if an exception occurs

Murach’s Visual Basic 2008, C7

Murach’s Visual Basic 2008, C7

Slide 17

Murach’s Visual Basic 2008, C7

© 2008, Mike Murach & Associates, Inc.

Slide 18

3

10/5/2008

A Function procedure that throws a FormatException

The syntax for throwing a new exception

Private Function FutureValue( _ monthlyInvestment As Decimal, _ monthlyInterestRate As Decimal, months As Integer) _ As Decimal If monthlyInvestment

Suggest Documents