BIM313 – Advanced Programming Techniques Debugging 1.
-
Upload
geraldine-payne -
Category
Documents
-
view
220 -
download
6
Transcript of BIM313 – Advanced Programming Techniques Debugging 1.
BIM313 – Advanced Programming Techniques
Debugging
1
Contents
• Debugging and Error Handling– Debugging feature in Visual Studio– try … catch … finally
2
Debugging and Error Handling
3
Error Types
• Syntax Errors– Forgetting semicolon, not closing parentheses or
curly braces, writing variable name incorrectly, etc.– Program is not compiled– Examine the compiler errors and correct the codes
• Logical Errors– Program is compiled– Program does not give the desired output– Debug your program!
4
Debugging in Visual Studio
• Put breakpoints in your code– F9, click at the beginning of a line, Debug menu
• Start your program in Debug mode– F5 (not Ctrl-F5), Play arrow in toolbar, Debug
menu• Press F10 for single step• Press F11 to enter a function• End debugging with Shift-F5 or Stop in toolbar
5
Monitoring Variable Content
• Move cursor an a variable while program is being debugged
• Follow the variables in the Locals and Autos windows
• Add variables into the Watch windows• Use the Immediate Window– Write the name of the variable and press Enter– You can call functions and expressions too
6
Configurations
• Debug– Extra symbols are included in the executable file so
that it can be debugged– Runs slowly– Executable file size larger
• Release– Runs faster– Executable file size smaller– Use this configuration on the last build (before
distributing your program)7
Debugging Without a Debugger
• A debugger (e.g. Visual Studio) makes debugging easier
• If you don’t have an IDE, you can use Console.WriteLine() to debug your programs
• Just call Console.WriteLine() to print variables or some useful messages
• Don’t forget to remove those lines in the last build
8
Debugging in Nonbreaking (Normal) Mode
Console.WriteLine("MyFunc() Function about to be called.");MyFunc("Do something.");Console.WriteLine("MyFunc() Function execution completed.");
9
Some Advanced Topics
• Debug.WriteLine() / Trace.WriteLine()• Debug.WriteLineIf() / Trace.WriteLineIf()• Debug.Assert() / Trace.Assert()• using System.Diagnostics;• You can set number of hits and hit condition
for breakpoints– Just right-click on the red dot of the breakpoint
10
Error Handling (try..catch..finally)
• While running a program through the IDE (i.e. during the development step, or Debug mode), you receive an error message if an error occurs.
• However, if your program runs in the final version (or Release mode), an error causes your program to terminate.
• For these cases, you may prefer using structured exception handling (or try..catch..finally)
11
Sections of the try Structure
• try: The try section is where you place code that might cause an exception.
• catch: Code within the catch section executes only when an exception occurs.
• finally: Code within the finally section occurs when the code within the try and/or catch sections completes. This section is where you place your cleanup code—code that you always want executed, regardless of whether an exception occurs.
12
try Example
int number = int.Parse(Console.ReadLine());int result = 100 / number;string str = result.ToString();
13
An error may occur while converting a
text into an integer!
An error may occur if the number is
zero!
try Example – Solution 1
try{ int number = int.Parse(Console.ReadLine()); int result = 100 / number; string str = result.ToString();} catch { Console.WriteLine(“An error has occurred!”);}
14
try Example – Solution 2try{ int number = int.Parse(Console.ReadLine()); int result = 100 / number; string str = result.ToString();} catch (ArgumentException ex1){ Console.WriteLine(“An ArgumentException error occurred!”);} catch (DivideByZeroException ex2) { Console.WriteLine(“The divisor can’t be zero”);} catch (Exception ex) { Console.WriteLine(“An unspecified error occurred. The details is: ” +
ex.Message);}
15
try Example – Solution 3
int result = 0;string strResult;try{ int number = int.Parse(Console.ReadLine()); result = 100 / number;} catch (Exception ex) { Console.WriteLine(“An unspecified error occurred. The details is: ” +
ex.Message);} finally { strResult = result.ToString();}
16
try Example on Database Operations
try{ … // Open database … // Make some operations on the database} catch { … // Error handling statements} finally { … // Close the database}
17