|
Debugging VB .NET Applications
In this section we will focus on some common issues while debugging our applications
and we will take a close look at the Debug Menu Visual Studio .NET provides us. Errors
in programming are inevitable. Everyone of us introduce errors while coding. Errors
are normally referred to as bugs. The process of going through the code to identify
the cause of errors and fixing them is called Debugging.
Types of Errors
Syntax Errors
The most common type of errors, syntax errors, occur when the compiler cannot compile
the code. Syntax errors occur when keywords are typed incorrectly or an incorrect
construct is parsed. The image below displays a syntax error in the code editor.
Fixing Syntax Errors
VS .NET allows us to easily identify syntax errors. When we build the project, syntax
errors are automatically detected and underlined in code. Errors that are detected
are also added to the Task List window. You can double-click any error in the Task
List window and the cursor will immediately highlight that error in the code window.
In most cases this is sufficient to correct the errors.
Run-Time Errors
Run-Time errors occur when the application attempts to perform a task that is not
allowed. This includes tasks that are impossible to carry out, such as dividing by
zero, etc. When a run-time error occurs, an exception describing the error is thrown.
Exceptions are special classes that are used to communicate error states between different
parts of the application. To handle run-time errors, we need to write code (normally,
using Try...Catch...Finally) so that they don't halt execution of our application.
The image below shows a run time error.
Logical Errors
Logical Errors occur when the application compiles and executes correctly, but does
not output the desired results. These are the most difficult types of errors to trace
because there might be no indications about the source of the error. The only way
to detect and correct logical errors is by feeding test data into the application
and analyzing the results. The image below describes a common scenario where logical
errors occur.
Assume, the image above performs some calculations for the purpose of taxation. It
multiplies three numbers. First number is the hourly wage an employee gets, second
number is the number of weeks in a year and third number is the number of hours a
week (40 hrs fulltime) the employee works. What happens here is, the code still executes
correctly and produces the output but the produced output is not the desired result
as the third number in code is 400 instead of 40. The output shown is 249 grand instead
of 24 grand. This is a simple scenario where logical errors occur.
|