Danh mục tài liệu

Lecture Windows programming: Chapter 5 - Châu Thị Bảo Hà

Số trang: 29      Loại file: pptx      Dung lượng: 300.84 KB      Lượt xem: 19      Lượt tải: 0    
Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Chapter 5 of lecture Windows programming introduce about debugging and error handling. In this chapter presents the contents: Introduction to the errors in program, debugging, error handling. Inviting you to refer.
Nội dung trích xuất từ tài liệu:
Lecture Windows programming: Chapter 5 - Châu Thị Bảo HàDebuggingandErrorHandlingChapter5 Ebook: Beginning Visual C# 2010, chapter 7 Reference: CSharp How to Program, part DContents Introduction Debugging Error handling Slide2Introduction Errors in program: a program can have three types of errors:  compile-time errors: The compiler will find syntax errors and other basic problems  run-time errors: A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally  logical errors: A program may run, but produce incorrect results, perhaps using an incorrect formula 1-3Debugging Debugging is the process of finding and correcting logic errors in applications  applications are executed in two ways: with debugging enabled (F5) or without debugging enabled (Ctrl+F5) Techniques  debugging in nonbreak mode  outputting debugging information: Console.WriteLine(), Debug.WriteLine()  debugging in break mode  using Breakpoints Some windows in DebugWindows: Autos, Locals, Watch  demo… Slide4Errorhandling An exception is an indication of a problem that occurs during a programs execution  format error,  arithmetic overflow,  out-of range array subscripts,  division by zero,  invalid method parameters  running out of available memory,  … Your program should be able to handle these exceptional situations. This is called exception handling Slide5Errorhandling(cont.)int tu = Convert.ToInt32( txtNumerator.Text );int mau = Convert.ToInt32( txtDenominator.Text );int result = tu / mau;lblOutput.Text = result.ToString(); Slide6Exceptionhandling:try…catch try{ // code that may cause exception } catch ( ExceptionTypeA e ){ // statement to handle errors occurring // in the associated try block } catch ( ExceptionTypeB e ){ // statement to handle errors occurring // in the associated try block } Slide7Exceptionhandling:try…catch...finally try{ // code that may cause exception } catch ( ExceptionTypeA e ){ // statement to handle errors occurring // in the associated try block } catch ( ExceptionTypeB e ) { … } finally{ // statements always excuted } Slide8 Exceptiontypes  Some common exception classesException Class CauseDivideByZeroException An attempt was made to divide by zero.FormatException The format of an argument is wrong.IndexOutOfRangeException An array index is out of bounds. An attempt was made to cast to an invalidInvalidCastExpression class.OutOfMemoryException Not enough memory to continue execution.StackOverflowException A stack has overflown. Slide9Example:try…catchprivate void divideButton_Click( object sender, System.EventArgs e ) { try { lblOutput.Text = ; int x = Convert.ToInt32( txtX.Text ); int y = Convert.ToInt32( txtY.Text ); int result = x / y; lblOutput.Text = result.ToString(); } catch ( FormatException ) { MessageBox.Show( You must enter two integers, Invalid NumberFormat, MessageBoxButtons.OK, MessageBoxIcon.Error ); } catch ( DivideByZeroException er ) { MessageBox.Show( er.Message, Attempted to Divide by Zero, MessageBoxButtons.OK, MessageBoxIcon.Error ); Slide10Thethrowstatement A throw statement explicitly generates an exception in code There are two ways you can use the throw statement  rethrow the exception in a catch block: catch( Exception e ) { // Add code to create an entry in event log throw; }  throw explicitly created exceptions: string strMessage = “EndDate should be greater than the StartDate”; ArgumentOutOfRangeException exp = Slide11 new ArgumentOutOfRangeException( strMessage );Definingpropertieswiththrowstatement[1] What should you public class DiemMonHoc { do if an invalid private string mMaSV; value is used? private string mMaMon; ...