Chapter 3 :
Error Handling for the User
Interface
Slide 2
9:06:37 AM
UNDERSTANDING EXCEPTIONS
•
An exception occurs when a program
encounters any unexpected problems.
•
Your program should be able to handle these
exceptional situations and, if possible,
gracefully recover from them. This is called
exception handling.
Slide 3
9:06:37 AM
STEP BY STEP 3_1
Slide 4
9:06:37 AM
STEP BY STEP 3_1
Slide 5
9:06:37 AM
UNDERSTANDING EXCEPTIONS
•
The FCL provides two categories of exceptions
–
ApplicationException Represents exceptions
thrown by the applications
–
SystemException Represents exceptions thrown by
the CLR
Slide 6
9:06:37 AM
HANDLING EXCEPTIONS
•
The try Block
Slide 7
9:06:37 AM
HANDLING EXCEPTIONS
•
The catch Block
DivideByZeroExceptionArithmeticException
OverflowException
FormatException
Slide 8
9:06:37 AM
HANDLING EXCEPTIONS
Slide 9
9:06:37 AM
HANDLING EXCEPTIONS
Slide 10
9:06:37 AM
HANDLING EXCEPTIONS
•
The throw Statement
Slide 11
9:06:37 AM
The throw Statement
Slide 12
9:06:37 AM
The throw Statement
Slide 13
9:06:37 AM
HANDLING EXCEPTIONS
•
The finally Block
–
The finally block contains code that always
executes, whether or not any exception occurs.
Ex: page _ _ _
See Demo
Slide 14
9:06:37 AM
VALIDATING USER INPUT
•
Field-Level Validation
–
1. Enter (Occurs when a control is entered.)
–
2. GotFocus (Occurs when a control receives focus.)
–
3. Leave (Occurs when focus leaves a control.)
–
4. Validating (Occurs when a control is validating.)
–
5. Validated (Occurs when a control is finished
validating.)
–
6. LostFocus (Occurs when a control looses focus.)
Slide 15
9:06:37 AM
The Validating Event
•
Inside the Validating event, you can write code to do
the following:
–
Programmatically correct any errors or omissions made by
the user.
–
Show error messages and alerts to the user so that the user
can fix the problem
–
Use the Focus() method of the control to transfer the focus
back to the field.
–
Set the Cancel property of CancelEventArgs to true. This
cancels the Validating event, leaving the focus in the
control.
Slide 16
9:06:37 AM
The CausesValidation Property
•
The default value of the CausesValidation
property for a control is true for all controls
•
When you want a control to respond,
regardless of the validation status of other
controls, you should set the CausesValidation
property of that control to false
Slide 17
9:06:37 AM
The ErrorProvider Component
•
The ErrorProvider component can set a small
icon next to a field when it contains an error
•
When the user moves the mouse pointer over
the icon, an error message pops up as a
ToolTip
Slide 18
9:06:37 AM
The ErrorProvider Component
Slide 19
9:06:37 AM
Using the ErrorProvider Component and
Other Validation Techniques
Slide 20
9:06:37 AM
The Validating Event and Sticky Form
–
The CausesValidation property of the btnExit control
to false.
–
Declare the following variable outside a method
block in the class:
private bool closingFlag = false;
–
Add the following code to the Click event handler of
the Exit button:
Slide 21
9:06:37 AM
The Validating Event and Sticky Form
•
Attach the following event handling code to the
Validating events of both the txtMile controls
Methods
Slide 23
9:06:37 AM
Math Class Methods
•
The Math class
–
Allows the user to perform common math
calculations
–
Using methods
•
ClassName.MethodName( argument1,
arument2, … )
–
Constants
•
Math.PI = 3.1415926535…
•
Math.E = 2.7182818285…
Slide 24
9:06:37 AM
Math Class Methods
Slide 25
9:06:37 AM
Math Class Methods