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.

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

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

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

11.1 Introduction to Error Handling
An Example:

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.

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.

11.2 Unstructured Exception Handling
On Error statements:

Try..Catch..Finally statements:
Try …. Try Code …. Catch ex As Exception …. Catch Code …. [Additional Catch blocks] Finally …. Finally Code …. End Try

11.3 Structured Exception Handling

The Try Block:

11.3 Structured Exception Handling

 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.

The Catch Block:

11.3 Structured Exception Handling

 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.

The Finally Block:

11.3 Structured Exception Handling

 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.

11.3 Structured Exception Handling
An illustration

The .NET framework Exception class:

11.3 Structured Exception Handling

 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.

The .NET framework Exception class:
 ArgumentException : an argument to a method is invalid

11.3 Structured Exception Handling

 SystemException : a failed run time check

 ArgumentNullException : a null argument is passed to a method that does not accept it.  ArgumentOutOfRangeException: argument value is out of range

The .NET framework Exception class:

11.3 Structured Exception Handling

 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.

The .NET framework Exception class:
 OutOfMemoryException: not enough memory to continue execution.  StackOverflowException : A stack has overflowed.

11.3 Structured Exception Handling

11.4 The Err Object
Properties 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.

11.4 The Err Object
Properties of an Err object:
 HelpFile: The link to the appropriate VB help file.  HelpContext: The VB HelpFile context ID corresponding to the error number.

11.4 The Err Object
An illustration:

11.5 Debugging
What 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

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.

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.

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.

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.

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.

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.

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.

11.5 Debugging
Immediate Window: • While in the debugger mode, you can use the immediate window to check/change the values of variables