29
11.1 Introduction to Error Handling Three types of errors: Syntax error – failure of code to meet the requirements of VB.NET programming languages. Examples : Not calling a procedure with the right number of parameters, misspelling a variable name.

VB Net Debugging

  • Upload
    juju157

  • View
    1.927

  • Download
    7

Embed Size (px)

DESCRIPTION

Teach beginner how to debug in Vb.Net

Citation preview

Page 1: VB Net Debugging

11.1 Introduction to Error Handling

Three types of errors:

Syntax error – failure of code to meet the requirements of VB.NET programming languages.

Examples :Not calling a procedure with the right number of parameters,

misspelling a variable name.

Page 2: VB Net Debugging

11.1Introduction to Error Handling

Three types of errors:

Compiler will flag syntax errors and output an error message in the output window.

If Option Explicit or even Option Strict turned on, syntax errors will be highlighted in the code window even before you compile

Page 3: VB Net Debugging

11.1 Introduction to Error Handling

Three types of errors:

Runtime Errors – appear while you are running the application.

Examples: Memory run out of space, Program caught into indefinite loop

Page 4: VB Net Debugging

11.1 Introduction to Error Handling

Three types of errors:

Logic Errors – will not cause program not to compile or run. Instead, it causes application to produce incorrect results.

Example: 2 + 2 returns a result of 22 instead of 4

Page 5: VB Net Debugging

11.1 Introduction to Error Handling

An Example:

Page 6: VB Net Debugging

11.2 Unstructured Exception Handling

On Error statements:

On Error GoTo Line or Label:

When an error occurs in run time, the execution will branch to the specified line label or line number in the On Error statement.

The specified line must be in the same procedure as the On Error statement.

Page 7: VB Net Debugging

11.2 Unstructured Exception Handling

On Error statements:

On Error Resume Next:

When an error occurs in run time, the execution will stop and immediately jump to the next statement after the one where the error has occurred.

The specified line must be in the same procedure as the On Error statement.

Page 8: VB Net Debugging

11.2 Unstructured Exception Handling

On Error statements:

Page 9: VB Net Debugging

11.3 Structured Exception Handling

Try..Catch..Finally statements:

Try…. Try Code ….

Catch ex As Exception…. Catch Code ….

[Additional Catch blocks]

Finally…. Finally Code ….

End Try

Page 10: VB Net Debugging

11.3 Structured Exception Handling

The Try Block:

The Try block is the place where the code is tried and checked for any generation of errors.

The Try block must be accompanied by at least one Catch block or one Finally block.

Page 11: VB Net Debugging

11.3 Structured Exception Handling

The Catch Block:

When an exception is raised in the Try block, the program will look for a match between the thrown exception’s type with each Catch handler’s exception parameter type.

The code in the matched Catch block will be executed and the rest of the Catch handlers are ignored.

Page 12: VB Net Debugging

11.3 Structured Exception Handling

The Finally Block:

Regardless of whether any error was raised in the Try block, the code within the Finally block will be executed

The Finally block is used to clean up the state before giving up the control to another part of the program.

Page 13: VB Net Debugging

11.3 Structured Exception Handling

An illustration

Page 14: VB Net Debugging

11.3 Structured Exception Handling

The .NET framework Exception class:

Whenever an error occurred, an exception will be generated

All exceptions can be found in the .NET framework Common Runtime Library

Let us take a look at some of the common exception classes on the next slide.

Page 15: VB Net Debugging

11.3 Structured Exception Handling

The .NET framework Exception class:

SystemException : a failed run time check

ArgumentException : an argument to a method is invalid

ArgumentNullException : a null argument is passed to a method that does not accept it.

ArgumentOutOfRangeException: argument value is out of range

Page 16: VB Net Debugging

11.3 Structured Exception Handling

The .NET framework Exception class:

ArithmeticException : arithmetic overflow or underflow has occurred.

DivideByZeroException : an attempt is made to divide by zero.

IndexOutOfRangeException : an array index is out of bounds.

NullReferenceException: attempt to use an unassigned reference.

Page 17: VB Net Debugging

11.3 Structured Exception Handling

The .NET framework Exception class:

OutOfMemoryException: not enough memory to continue execution.

StackOverflowException : A stack has overflowed.

Page 18: VB Net Debugging

11.4 The Err ObjectProperties of an Err object: Number: The error number describes the cause of the error. Range: 0 to 65535

Source: Describes the name of the current VB project

Description: A string of text which corresponds to the error number.

Page 19: VB Net Debugging

11.4 The Err ObjectProperties of an Err object: HelpFile: The link to the appropriate VB help file.

HelpContext: The VB HelpFile context ID corresponding to the error number.

Page 20: VB Net Debugging

11.4 The Err Object

An illustration:

Page 21: VB Net Debugging

11.5 DebuggingWhat is debugging?

It is an art of identifying and ridding your application of programming errors (bugs)

Debugging logic errors is an important skill

VB.NET provides a number of debugging tools

Page 22: VB Net Debugging

11.5 Debugging

Setting Breakpoints

• When a program is running in the debugger, a breakpoint will halt execution and give the developer control of the debugger.

• Lines with a breakpoint are highlighted in red. You can remove a breakpoint by right-clicking the line again and selecting Remove Breakpoint.

Page 23: VB Net Debugging

11.5 Debugging

To set a breakpoint, right-click the line where you want the program to halt and click Insert Breakpoint as shown here.

Page 24: VB Net Debugging

11.5 Debugging

Stepping Thru a Program:

• Once a breakpoint is set, the program can be run in the debugger.

• In the Debug menu, select Start instead of Start Without Debugging

• This starts your program in the debugger, and the breakpoints will be enabled.

Page 25: VB Net Debugging

11.5 Debugging

• Once the program hits your breakpoint, the debugger receives control of the program. There will be an arrow pointing to the current line of execution.

Page 26: VB Net Debugging

11.5 Debugging

• To step through one line of code, select Debug | Step Over and watch the cursor move to the next line.

• The Debug | Step Into command allows you to step into a function that is about to be called.

Page 27: VB Net Debugging

11.5 Debugging

Inspecting Variable Values• While you have control of the debugger, you

can move your mouse cursor over a variable to obtain its basic value.

Page 28: VB Net Debugging

11.5 Debugging

QuickWatch• You can also right-click the variable and select

QuickWatch from the context menu. The QuickWatch gives you greater detail for certain variables, such as an ArrayList object.

Page 29: VB Net Debugging

11.5 Debugging

Immediate Window:• While in the debugger mode, you can use the

immediate window to check/change the values of variables