Slide 139:06:37 AM HANDLING EXCEPTIONS – The finally block contains code that always executes, whether or not any exception occurs... This cancels the Validating event, leaving the foc
Trang 1Chapter 3 :
Error Handling for the User
Interface
Trang 2UNDERSTANDING 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
Trang 3Slide 3
9:06:37 AM
STEP BY STEP 3_1
Trang 4STEP BY STEP 3_1
Trang 5Slide 5
9:06:37 AM
UNDERSTANDING EXCEPTIONS
• The FCL provides two categories of exceptions
thrown by the applications
the CLR
Trang 6HANDLING EXCEPTIONS
Trang 7Slide 7
9:06:37 AM
HANDLING EXCEPTIONS
DivideByZeroException ArithmeticException FormatException OverflowException
Trang 8HANDLING EXCEPTIONS
Trang 9Slide 9
9:06:37 AM
HANDLING EXCEPTIONS
Trang 10HANDLING EXCEPTIONS
Trang 11Slide 11
9:06:37 AM
The throw Statement
Trang 12The throw Statement
Trang 13Slide 13
9:06:37 AM
HANDLING EXCEPTIONS
– The finally block contains code that always
executes, whether or not any exception occurs
Ex: page _ _ _ See Demo
Trang 14VALIDATING 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.)
Trang 15Slide 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.
Trang 16The 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
Trang 17Slide 17
9:06:37 AM
The ErrorProvider Component
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
Trang 18The ErrorProvider Component
Trang 19Slide 19
9:06:37 AM
Using the ErrorProvider Component and
Other Validation Techniques
Trang 20The Validating Event and Sticky Form
– The CausesValidation property of the btnExi t 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 :
Trang 21Slide 21
9:06:37 AM
The Validating Event and Sticky Form
• Attach the following event handling code to the
Validating events of both the txt Mile controls
Trang 22Methods
Trang 23Slide 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…
Trang 24Math Class Methods
Max( -2.3, -12.7 ) is -2.3
Trang 25Sqrt( x ) Sqrt( 900.0 ) is 30.0
Sqrt( 9.0 ) is 3.0 Tan( x ) Tan( 0.0 ) is 0.0
Trang 26• Contains the code of what the method does
• Contains the return value if necessary
– For uses call elsewhere in program
• Pass parameters if needed
Trang 27//Contains the code of what the method does
//Contains the return value
}
Trang 28User-defined method Maximum (p 196)
public double Maximum( double x, double y, double z )
Trang 29Slide 29
9:06:37 AM
User-defined method Maximum (p 195)
public void DetermineMaximum()
{
Console.WriteLine( "Enter three floating-point values,\n"
+ " pressing 'Enter' after each one: " );
double number1 = Convert.ToDouble( Console.ReadLine() );
double number2 = Convert.ToDouble( Console.ReadLine() );
double number3 = Convert.ToDouble( Console.ReadLine() );
double result = Maximum( number1, number2, number3 );
Console.WriteLine( "Maximum is: " + result );
}
Trang 30Argument Promotion
• Implicit Conversion
– Object is converted to a needed type implicitly
– Only done if complier knows no data will be lost
• Explicit Conversion
– Object is manually converted
– Required if there could be a loss of data
– Widening
• Make an object that of a derived class and more complex
Trang 31byte decimal, double, float, int, uint, long, ulong, object, short or ushort
sbyte decimal, double, float, int, long, object or short
char decimal, double, float, int, uint, long, ulong, object or ushort
decimal object
double object
float double or object
int decimal, double, float, long or object
uint decimal, double, float, long, ulong, or object
long decimal, double, float or object
ulong decimal, double, float or object
object None
short decimal, double, float, int, long or object
ushort decimal, double, float, int, uint, long, ulong or object
Trang 32Passing Arguments: Call-By-Value vs
Call-By-Reference (p 219)
• Passing by value
– Send a method a copy of the object
– When returned are always returned by value
– Set by value by default
• Passing by reference
– Send a method the actual reference point
• Causes the variable to be changed throughout the program
– When returned are always returned by reference
Trang 33Slide 33
9:06:37 AM
Reference and value parameters
void SquareRef( ref int x )
Value of z after Square: 5
Value of z after SquareRef: 25
Value of z after SquareOut: 25
Trang 34Random Number Generation
Trang 35Slide 35
9:06:37 AM
int value;
value = rand Next();// phát sinh 1 số trong [0;
2,147,483,647 value = rand Next( 6 );// phát sinh 1 số trong [0; 5] value = rand Next( 1, 7 );// phát sinh 1 số trong [1,6]
Trang 36Finish