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; ...
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; ...
Tìm kiếm theo từ khóa liên quan:
Lecture Windows programming Lập trình Windows Bài giảng Lập trình Windows Windows programming Errors in program Error handlingTài liệu có liên quan:
-
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 204 0 0 -
bảo mật mạng các phương thức giả mạo địa chỉ IP fake IP
13 trang 169 0 0 -
Bài giảng Lập trình Windows Form với C#: Chương 3 - Lê Thị Ngọc Hạnh
11 trang 157 0 0 -
Excel add in development in c and c phần 9
0 trang 124 0 0 -
information technology outsourcing transactions process strategies and contracts 2nd ed phần 3
65 trang 116 0 0 -
Giáo trình Lập trình Windows 1 - Trường CĐN Đà Lạt
117 trang 104 0 0 -
Hướng dẫn lập trình OpenGL căn bản
33 trang 60 0 0 -
A guide to disability insurance
20 trang 59 0 0 -
thủ thuật windows XP hay nhất phần 2
14 trang 47 0 0 -
The CISA Prep Guide Mastering the Certified Information Systems Auditor Exam phần 1
60 trang 46 0 0