Pre-Assessment Questions: Exception Handling, Debugging and Testing Windows Applications
Pre-Assessment Questions: Exception Handling, Debugging and Testing Windows Applications
Windows Applications
Pre-assessment Questions
1. Which of the following is a valid specific culture:
a. fr
b. de
c. fr-France
d. fr-Fr
2. Which of the following involves writing code in such a way that it is culture
and language neutral?
a. Globalization
b. Localization
c. Culture-specific formatting
d. Deployment
Solutions to Pre-assessment
Questions
1. d. fr-Fr
2. a. Globalization
3. a. Encoding.GetEncoding
4. b. AccessibleName
5. d. LanguageInfo
Objectives
In this lesson, you will learn to:
• Handle and raise exceptions
• Develop test plans
• Configure the debugging environment
• Use the Break mode to debug an application
• Use debugger windows to trace and debug an application
• Implement custom trace listeners
• Implement trace switches
Exception Handling
• An exception is termed as an abnormal condition encountered by an
application during execution.
• Exception handling is the process of providing an alternate path of execution
when the application is unable to execute in the desired way.
• By handling exceptions, you can prevent an application from terminating
abruptly when an error is encountered.
Types of Errors
• There are three types of errors that can occur in an application:
• Syntax Errors: A syntax error occurs when the compiler cannot
compile the code.
• Run-time Errors: A run-time error occurs when an application
attempts to perform an operation that is not allowed.
• Logical Errors: A logical error occurs when an application compiles
and runs properly but does not produce the expected results.
System.Exception
System.ApplicationException
System.Windows.Forms.AxHost.InvalidActiveXStateException
System.Runtime.Remoting.MetadataServices.SUDSParserException
System.IO.IsolatedStorage.IsolatedStorageException
System.Runtime.Remoting.MetadataServices.SUDSGeneratorException
System.SystemException
Structured Exception
Handling(Contd.)
• The exception classes derived from the System.Exception class are
discussed below:
• System.ApplicationException— This exception is thrown by user
created applications.
• System.Windows.Forms.AxHost.InvalidActiveXStateException— This
exception is thrown when the ActiveX control that is in an invalid state
is called by an application.
• System.Runtime.Remoting.MetadataServices.SUDSParserException—
This exception is thrown when the parsing process of Web Services
Description Language (WSDL) generates an error.
• System.IO.IsolatedStorage.IsolatedStorageException— This exception
is thrown when an error is generated in an isolated storage operation.
Structured Exception
Handling(Contd.)
• System.SystemException — This exception class acts as a base class for all
the predefined system exceptions. Some of the classes derived from this
class are given below:
• System.IO.IOException
• System.Data.DataException
• Sytsem.Data.SqlClient.SqlException
• System.IndexOutOfRangeException
• System.NullReferenceException
• In structured exception handling, the application is divided into blocks of
code.
Structured Exception
Handling(Contd.)
• The structure of the Try…Catch…Finally statement is given below:
Try
' This block includes the code that can raise an error.
Catch (optional filters)
'This block includes the code that runs if the code in
'The Try block gives an error and the filter on the Catch
'statement is true, if specified.
'(Additional Catch blocks)
Finally
'This block includes the code that will always run just before
the Try statement exits.
End Try
User-defined Exceptions
• In addition to handling pre-defined exceptions, users can create their own
exceptions.
• User-defined exception classes are derived from the ApplicationException
class.
Raising Exceptions
• There are instances when you want to raise exceptions.
• Exceptions can be explicitly thrown in an application by using the Throw()
method.
Debugging a Windows-based
Application
• When writing code for applications, errors may be introduced in programs,
these errors are called bugs.
• The process of locating and fixing bugs in a program is called Debugging.
Overview of Debugging
• Visual Basic .NET provides a set of tools that allow you to trace and correct
errors in your programs in the development environment itself.
• To assist you in debugging logical and run-time errors, Visual Basic.NET
provides extensive debugging tools.
• Another method to access the debugging tools is from the Debug menu.
• By using the debugging tools, you can perform the following actions:
• Start execution
• Break execution
• Step through execution
• Stop execution
Setting a Breakpoint
• A break point can be set on an executable line of code.
• To set a breakpoint, you need to identify the statement where the error
starts.
• You can remove a breakpoint from a code by selecting Remove
Breakpoint from the shortcut menu of the code line at which the
breakpoint is set.
Watch Window
• Using the Watch window, you can change the values of variables or
expressions in the break mode and observe how these different values affect
your code.
• Each variable or expression that you observe in the Watch window is called a
watch expression.
• Visual Basic. NET automatically monitors watch expressions.
• The Watch window can be displayed by selecting Windows from the Debug
menu and then selecting Watch1 from the Watch submenu.
Output Window
• The Output window is useful in displaying debug messages, such as build
errors and any user-defined messages during run time.
• The Output window is displayed by using the ViewOther WindowsOutput
option.
• You can display debug messages during run time by using the methods of
the Debug class.
• The Debug class is derived from System.Diagnostics namespace and contains
methods that you can use to debug applications.
Locals Window
• The Locals window is a debugging tool that allows you to view the values
present in the variables declared in the local procedures.
• To display the Locals window, select DebugWindowsLocals option when
debugging in the break mode.
Immediate Window
• The Immediate window is a debugging tool to check the values present in
the global and local variables.
• The Immediate window allows you to:
• Set the value of a variable to observe the change in the result based on
the value that is set.
• Create or destroy objects.
• To start the Immediate window select DebugWindowsImmediate to open
the Immediate window.
Demo
Using Breakpoints
Problem Statement
• An application has been created to display the product ID for a product that
costs more than $3000.
• A Windows Form has been designed with a ListBox control named lbprodid.
• A data adapter named OleDbDAProduct has been created, and the Base_Cost
and ProdID columns of the table Product have been retrieved.
• A dataset named DsProduct1 has been generated for the data adapter.
• The following code has been written for the Load event of the form to display
the ProductID of the products with cost more than $3000 in the list box.
Dim criteria As String
Dim sort As String
Dim dt As DataTable
Dim result() As DataRow
Dim ctr As Integer
OleDbDAProduct.Fill(DsProduct1)
dt = DsProduct1.Tables("Product")
criteria = "Base_Cost > '3000'"
©NIIT Enhancing and Distributing Applications Lesson 1B / Slide 31 of 50
Exception Handling, Debugging and Testing
Windows Applications
• However, the application does not give the expected output. The list box
displays one product ID multiple times.
Solution
• To locate the error in the above program, the following steps need to be
performed:
1. Identify the starting point to search for errors.
2. Set a breakpoint.
3. Run the application and trace the error.
4. Rectify the code.
5. Verify the output of the code.
Demo
Debugging a Windows-based
Application
Problem Statement
• An application has been developed that displays the corresponding base cost
and the product name of the product ID selected by the user from a list box.
• A data adapter named OleDbDAProduct has been created, and the
Base_Cost and ProdID columns of the table Product have been retrieved.
• A dataset named DSProduct has been generated for the data adapter.
• A ListBox control named lbprodid, two TextBox controls, and a Button control
with Text property set to Display have been added to the Form. The following
general level declarations have been made.
A ListBox control named lbprodid, two TextBox controls, and a Button control with
Text property set to Display have been added to the Form. The following
general level declarations have been made.
Dim dt As DataTable
Dim result(), resultarray As DataRow
Dim prodid, criteria, sort As String
Dim ctr As Integer
Solution
• To solve the preceding problem, the following steps need to be performed:
1. Identify the mechanism to debug the code.
2. Implement debugging.
3. Rectify the code.
4. Verify the output of the code.
Overview of Testing
• Testing measures the quality of the software you are developing.
• Testing is undertaken to find and resolve the defects in your application.
• Testing can be of various types, which include:
• Unit testing: Unit testing consists of isolating the smallest piece of
testable software from the remaining application and determining
whether it behaves as expected.
• Integration testing: Integration testing combines two or more tested
units into a component and tests the interface between them.
• Regression testing: In regression testing you rerun the existing tests
against the modified code to determine whether the changes have
affected anything that was working correctly before the modification of
code.
• Acceptance testing: During acceptance testing, users are allowed to use
the application and give feedback about the functionality, effectiveness,
and efficiency of the application as per the requirements.
Implementing Tracing
• Tracing is a technique that allows you to log informative messages about an
application’s conditions at run time.
• Using tracing, you can get informative messages about the execution of your
application without interrupting the application execution even after it has
been deployed.
• There are three phases of code tracing:
• Instrumentation: In the instrumentation phase, trace code is added to
your application.
• Tracing: In the tracing phase, the tracing code writes information about
the execution of the application to the specified target.
• Analysis: In the analysis phase, tracing information is evaluated to
identify and understand problems in the application.
Summary
In this lesson, you learned that:
• The types of errors that can occur in an application are syntax errors, run-
time errors, and logical errors.
• An exception is an abnormal condition that an application encounters
during execution.
• Exception handling is the process of providing an alternate path to be
executed when the application is not able to execute in the desired way.
• Visual Basic .NET provides two methods to handle exceptions:
• Structured exception handling
• Unstructured exception handling
• In addition to handling pre-defined exceptions, users can create their own
exceptions by deriving an exception class from the ApplicationException
class.
Summary (Contd.)
• Once an application has been coded, it must be tested to ensure that it
fulfills the requirements for which it was designed and meets quality
expectations.
• There are various types of testing that an application has to go through.
These include unit testing, integration testing, regression testing and
acceptance testing.
• For testing to be successful, a test plan must be created that outlines the
entire process of testing and includes budget, schedule and performance
considerations.
• In order to assist you in debugging logical and runtime errors, Visual Basic
.NET provides extensive debugging tools. You can use the Visual Basic
.NET Debug toolbar to access these tools.
• By using the debugging tools, you can perform the following actions:
• Start execution
• Step through execution
• Stop execution
©NIIT Enhancing and Distributing Applications Lesson 1B / Slide 49 of 50
Exception Handling, Debugging and Testing
Windows Applications
Summary (Contd.)
• You can log informative messages about your application’s conditions at
run time by using tracing.
• Tracing can be implemented by using the Trace and Debug classes.
• The Listeners collection is a group of classes capable of receiving output
from the Debug and Trace classes.
• Trace switches allow you to enable, disable, and filter tracing output.
• Conditional compilation is a means of selectively compiling portions of
your program code. This can be used for creating multiple versions of your
application without the need to maintain multiple copies of code.