Debugging Techniques Debugging Techniques 1 Introduction • • • • Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques Debugging Techniques 2 Bugs/Errors • Main types – Syntax error – Logic error – Runtime error Debugging Techniques 3 Syntax Error • A syntax error refers to a mistake in a statement's syntax. • A fatal error that the programmer must correct before being able to fully compile the program. • Can be indicated by the compilers • Some IDEs are able to display syntax errors in real-time. Debugging Techniques 4 Logic Error • A Logic Error is a bug in a program that causes unexpected results or operation but not failure. • Cannot be checked by the compilers, as the program is syntactically valid. • Logic errors are usually the hardest to debug because they do not cause the program to fail completely. • A common technique in solving logic errors is to print the program's variables to a file or the console in order to isolate where the problem is. Debugging Techniques 5 Runtime Error / Exception • A Runtime Error (Exception) is a problem that arise when the program is executed. • E.g. divide by zero, array out of bounds Debugging Techniques 6 How to Debug? • Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program. • The basic steps in debugging are: – – – – – Recognize that a bug exists Isolate the source of the bug Identify the cause of the bug Determine a fix for the bug Apply the fix and test it • Tools – Walk through your program – Display the variables at certain points, trace the program – Use of professional debugger Debugging Techniques 7 Testing Methods (1) • Exhaustive testing – Test a program with all possible data values – Not feasible • Black box (functional) testing – Treat the unit of testing as a Black Box (with inputs and outputs) only, and test its function – Black box testing takes an external perspective of the test object to derive test cases. The test designer selects valid and invalid input and determines the correct output. There is no knowledge of the test object's internal structure. – The key is to devise sample data that is representative of all possible data Debugging Techniques 8 Testing Methods (2) • White box (structural) testing – Make use of knowledge of how the program works, its structure – White box testing uses an internal perspective of the system to design test cases based on internal structure. It requires programming skills to identify all paths through the software. The tester chooses test case inputs to exercise all paths and determines the appropriate outputs. – Test all execution paths • Inspections and Walkthroughs Debugging Techniques 9 Visual Studio .NET Debugger Debugging Techniques 10 Stepping through Code • Modern debuggers usually allow you to step through your program and look at the values of variables • Also the debugger will allow you to set breakpoints, which are statements in your programs that you mark and tell the debugger to suspend program execution when it gets there • Tools – Start, Stop, Break, Restart – Step Into • Executes the next statement even if it’s inside a new procedure – Step Out • Finishes executing the current procedure and stops when control returns to the calling procedure – Step Over • Executes the next statement. If that statement is a procedure, the procedure is executed without stopping • Autos, Locals, Watch windows • Call Stack window Debugging Techniques 11 Exception Handling • Syntax errors can be caught by the compiler and signaled by the IDE • Runtime errors (exceptions) are problems that arise when the program is executed – – – – E.g. result = x / y This is a valid statement but what if y = 0? The program will be interrupted, and an exception will be thrown An Exception is an object of a special data type (Exception) that is generated as a result of the error and it contains information about the runtime error • Your program should handle the exceptions, instead of letting it dump • Two exception handling approaches – Unstructured Exception Handling – Structured Exception Handling Debugging Techniques 12 Unstructured Exception Handling • An older technique of dealing with exceptions • In previous versions of VB, an global object named Err is used to indicate the error • An error handler is a statement that tells the runtime what to do if an error occurs • Or using If statements to check for errors Debugging Techniques 13 Unstructured Exception Handling Module Module1 Sub Main() Dim i, j As Integer On Error Goto Main_Failed i=i\j Exit Sub Main_Failed: If err.Number = 11 Then ‘ DivideByZeroException j=1 Resume ‘ re-execute the same statement ElseIf err.Number = 10 Then Resume Next ‘ ignore the offending statement and continue Else MsgBox(Err.Description) End If End Sub End Module Debugging Techniques 14 Structured Exception Handling (1) • A modern approach to Exception Handling • Try-Catch statement • Try block consists of the statement(s) that you believe may cause exceptions to be thrown • Catch block is the exception handler which specifies what to do in case of error • If an exception occurs while executing the code in the Try block, the program execution is interrupted, and the control is passed to the appropriate Catch handler • There may be more than one catch block, each for a specific type of exception Debugging Techniques 15 Structured Exception Handling (2) Dim i As Integer = 5 Dim j As Integer = 0 Dim result As Integer Try result = i \ j Catch e As DivideByZeroException MsgBox("We caught a divide by 0 exception") End Try Debugging Techniques 16 Structured Exception Handling (3) • Can have multiple Catch blocks (Example ExceptionExample) Dim i As Integer = 5 Dim result As Integer Try result = Integer.MaxValue * i Catch e As DivideByZeroException MsgBox("We caught a divide by 0 exception") Catch e As Exception MsgBox("We caught generic exception") System.Console.Out.WriteLine(e.StackTrace) Throw ‘ re-throw for some other handler to deal with it End Try Debugging Techniques 17 Structured Exception Handling (4) • Exceptions are propagated on the call stack (Example ExceptionExample2) Module Module1 Sub Main() Dim result As Integer Try result = MultiplyInts(Integer.MaxValue, 5) Catch e As DivideByZeroException MsgBox("We caught a divide by 0 exception") Catch e As Exception MsgBox("We caught generic exception") System.Console.Out.WriteLine(e.StackTrace) End Try End Sub Function MultiplyInts(ByVal i As Integer, ByVal j As Integer) As Integer Dim result As Integer result = i * j Return result End Function End Module Debugging Techniques 18 Structured Exception Handling (5) Try … Catch … Finally ‘ Code that will always be executed End Try Debugging Techniques 19