Week 7: Debugging and Error Handling Debugging methods available in the ID Error-handling techniques available in C#

Slides:



Advertisements
Similar presentations
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
C# Programming: From Problem Analysis to Program Design1 Debugging and Handling Exceptions C# Programming: From Problem Analysis to Program Design 3 rd.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Chapter 11 Debugging and Handling Exceptions
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Example 2.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Finding and Debugging Errors
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
JavaScript, Fourth Edition
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
CHAPTER 6 Loop Structures.
Apply Sub Procedures/Methods and User Defined Functions
CHAPTER SIX.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Chapter 12: Exception Handling
Tutorial 11 Using and Writing Visual Basic for Applications Code
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Introduction to Exception Handling and Defensive Programming.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
CPS120: Introduction to Computer Science Functions.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
C# - FCL/Form & Control Validation John Kelleher.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming with Microsoft Visual Basic th Edition
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
C# Programming Methods.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
A variable is a name for a value stored in memory.
Debugging and Handling Exceptions
How to debug an application
Why exception handling in C++?
Computer Programming I
Advanced Programming Chapter 7: Methods: A Deeper Look
Debugging Visual Basic Programs
Debugging and Handling Exceptions
Presentation transcript:

Week 7: Debugging and Error Handling Debugging methods available in the ID Error-handling techniques available in C#

Debugging methods available in the ID Week 7: Debugging and Error Handling

DEBUGGING IN VS AND VCE Both VS and VCE(express) allow you to build applications in two configurations: Debug (the default) and Release. Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 3

DEBUGGING IN VS AND VCE In debug configuration and execute it in debug mode, more is going on than the execution of your code. Debug builds maintain symbolic information about your application, so that the IDE knows exactly what is happening as each line of code is executed In release configuration, application code is optimized. However, release builds also run faster; and when you have finished developing an application, you will typically supply users with release builds because they won’t require the symbolic information thatdebug builds include Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 4

Debug Menu and Toolbar Breakpoints Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 5

Breakpoints Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 6 Toggle Breakpoints On/Off by clicking in Editor's gray left margin indicator

Debugging in Break Mode Breakpoints Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 7

Monitoring Variable Content Monitoring variable content is just one example of how VS and VCE help you a great deal by simplifying things. The easiest way to check the value of a variable is to hover the mouse over its name in the source code while in break mode. A yellow tooltip showing information about the variable appears, including the variable’s current value. Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 8

Viewing Current Values During Program Execution Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 9 Place mouse pointer over variable or property to view current value

Monitoring Variable Content Autos (VS only): Variables in use in the current and previous statements (Ctrl+D, A) Locals: All variables in scope (Ctrl+D, L) Watch N: Customizable variable and expression display (where N is 1 to 4, found on Debug WindowsWatch) Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 10

Locals window Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 11

The Watch window Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 12

Immediate and CommandWindows The Command (VS only) and Immediate windows (found on the DebugWindows menu) enable you to execute commands while an application is running. The Command window enables you to perform VS operations manually (such as menu and toolbar operations). The Immediate window enables youto execute additional code besides the source code lines being executed, and to evaluate expressions. Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 13

Immediate and CommandWindows Windows Programming 1 Chapter 7: Debugging and Error Handling Slide 14

Error-handling techniques available in C# Week 7: Debugging and Error Handling

Slide 16 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 17 STEP BY STEP 3_1

Slide 18 STEP BY STEP 3_1

Slide 19 UNDERSTANDING EXCEPTIONS The FCL provides two categories of exceptions ApplicationException Represents exceptions thrown by the applications SystemException Represents exceptions thrown by the CLR

Slide 20 Try Block - General Form The try Block Try { statements that may cause error } catch [ExceptionType VariableName ] { statements for action when an exception occurs }

Slide 21 HANDLING EXCEPTIONS The catch Block DivideByZeroException ArithmeticException OverflowException FormatException

Slide 22 HANDLING EXCEPTIONS

Slide 23 HANDLING EXCEPTIONS

Slide 24 HANDLING EXCEPTIONS The throw Statement

Slide 25 The throw Statement

Slide 26 The throw Statement

Slide 27 HANDLING EXCEPTIONS The finally Block: contains code that always executes, whether or not any exception occurs.

Slide 28 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 29 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 30 VALIDATING USER INPUT 1. KeyDown 2. KeyPress 3. KeyUp The KeyPress event happens after the KeyDown event but before the KeyUp event KeyPress event match keys include any alphabetic and numeric characters (alphanumeric a–z, A–Z, and 0–9), not raise this event include Ctrl, Alt, and the function keys

Slide 31 VALIDATING USER INPUT EXAM private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar 57) && e.KeyChar != 8) e.Handled = true; } private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.Alt == true) MessageBox.Show("The ALT key is still down"); }

Slide 32 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 33 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 34 9:06:37 AM The ErrorProvider Component

Slide 35 Using the ErrorProvider Component and Other Validation Techniques

Slide 36 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 37 The Validating Event and Sticky Form Attach the following event handling code to the Validating events of both the txt Mile controls

Methods

Slide 39 Math Class Methods The Math class Allows the user to perform common math calculations Using methods ClassName.MethodName( argument1, arument2, … ) Constants Math.PI = … Math.E = …

Slide 40 Math Class Methods

Slide 41 Math Class Methods

Slide 42 9:06:37 AM Method Definitions Writing a custom method Header ReturnType Properties Name( Param1, Param2, …) Body Contains the code of what the method does Contains the return value if necessary For uses call elsewhere in program Pass parameters if needed All methods must be defined inside of a class

Slide 43 Method Definitions public void MethodName ( ) { // Contains the code of what the method does } public ReturnType methodName(Param1, Param2, … ) { //Contains the code of what the method does //Contains the return value }

Slide 44 User-defined method Maximum public double Maximum( double x, double y, double z ) { double maximumValue = x; if ( y > maximumValue ) maximumValue = y; if ( z > maximumValue ) maximumValue = z; return maximumValue; } // end method Maximum

Slide 45 9:06:37 AM User-defined method Maximum 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 ); }

Slide 46 Argument 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 Narrowing Make an object that of a base class and cause some data loss

Slide 47 Argument Promotion

Slide 48 Passing Arguments: Call-By- Value vs. Call-By-Reference 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 The ref keyword specifies by reference The out keyword means a called method will initialize it

Slide 49 Reference and value parameters void SquareRef( ref int x ) { x = x * x; } void Square( int x ) { x = x * x; } void SquareOut( out int x ) {x=5; x = x * x; } int z=5; Square ( z ); int z=5; SquareRef (ref z ); int z; SquareOut (out z ); Value of z after Square: 5 Value of z after SquareRef: 25 Value of z after SquareOut: 25

Slide 50 Random Number Generation Class Random Within namespace System randomObject.Next () Returns a number from 0 to Int32.MaxValue  Int32.MaxValue = 2,147,483,647 randomObject.Next ( x ) Returns a value from 0 up to but not including x randomObject.Next ( x, y ) Returns a number between x and up to but not including y

Slide 51 Class Random_ Example Random rand = new Random(); 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]