Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

270

Project 1

DATABASE PROGRAMMING USING VC++.NET

 

 

 

semantic errors. The very familiar runtime errors are a type of semantic

 

 

 

 

 

 

error. If you were to examine the reason behind this classification, you

 

 

 

would realize that runtime errors occur when the code attempts to per-

 

 

 

form an illegal operation. For instance, division by zero is a runtime error

 

 

 

because this is an invalid operation. Another example is the “Memory

 

 

 

fault-core dumped” error message. You have encountered this error mes-

 

 

 

sage if you have used any array or pointer variable in a C or a C++ pro-

 

 

 

 

 

 

Y

 

 

 

gram. Why? Arrays and pointer variables deal directly with memory, so

 

 

 

 

 

L

 

 

 

when you forget to allocate memory and use the same variable to refer to

 

 

 

 

 

F

 

 

 

a memory location, the program terminates with the “Memory fault-core

 

 

 

dumped” message. In simple words, semantic errors occur when your

 

 

 

 

 

M

 

 

 

 

code attempts to perform any invalid operation. These errors terminate

 

 

 

 

A

 

 

 

 

the program and may or may not display any error messages.

 

 

 

 

E

 

 

 

 

 

 

 

 

 

NOTE

T

 

 

 

 

A compiler or an interpreter does not trap semantic errors, because it can only check

 

 

the code for syntax adherence, not decipher the meaning of code.

 

 

 

 

 

 

 

Logic errors. “Logic” forms the core of any program — no programmer can deny this fact, because the result of any program is based on its logic. Consider a simple example: You are allocated the task of creating a program that has to calculate the bonus amount as 6 percent of the given sales amount. To accomplish this, you write the following code:

void BonusCalc(int SalesAmt)

{

int BonusAmt;

BonusAmt = SalesAmt + (6/100);

}

The compilation is error-free, but when you run this code, you don’t get the desired output. For instance, a 6 percent bonus for a sales amount of $3500 should be $210, whereas the output of this code is $3500.06. Examining the code, you find that instead of multiplying the sales amount by 0.06, you have added the sales amount to 0.06. This is neither a syntax error nor a semantic error (because the program didn’t

Team-Fly®

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

271

 

 

 

 

 

terminate without any result). However, the program didn’t result in the desired output; hence, this error is classified as a logic error.

One of the much common logic errors, which any programmer would have encountered in their initial stages of programming, is the “infinite loop.” Often, while creating a loop, such as a for loop, we forget to increment or decrement the counter variable, and this results in an infinite loop.

Knowledge about the types of errors will help you to a great extent reduce the number of errors in your program. However, true to the statement “to err is human,” you do end up writing programs with some bugs. Don’t panic! All you need to do is to categorize the error and remove it. Is it so easy? Yes, Visual Studio .NET eases debugging by providing you with a set of highly sophisticated and efficient debugging tools. In addition to the debugging tools of Visual Studio 6.0, some new tools are now available in Visual Studio .NET . In the subsequent sections, you will learn about the tools offered by Visual Studio .NET and how to use them and debug Visual C++ applications.

Before getting into the nitty-gritty of error handling in Visual C++ .NET , you first need to spend some time learning the types of build configurations available in Visual Studio .NET .

Build Configurations

A build configuration helps you to specify the components that you want to build, determine how the project will be built, and determine the platform on which the project will be built. Visual Studio .NET supports two types of build configurations:

Solution build configurations. This helps you to specify how the projects in a solution are to be built and, if required, deployed. The solution build configuration contains an entry against each project in the solution, which specifies the project name, the type of build selected, and the platform on which the project will be built. You can edit the solution build configuration or define a new one. To do so, choose Build, Configuration Manager. In the Configuration Manager dialog box, shown in Figure 9-1, select New to define a new configuration or Edit to edit the existing one.

272 Project 1 DATABASE PROGRAMMING USING VC++.NET

FIGURE 9-1 The Configuration Manager dialog box

Project build configurations. Visual Studio .NET supports two types of project configurations: Debug and Release. Debug is the default configuration for new projects and facilitates easy application development by providing a number of debugging tools and classes for you to debug your application. Once a project is debugged and tested, and is ready for release, you can compile the project in the Release build. In the Release configuration, the compiler optimizes the code to a great extent by removing information related to debugging. Thus your code is reduced in size, and hence runs faster than code that is not optimized.

The Solution Configuration drop-down list on the Standard toolbar displays the configuration of the project (that is, the active project in the Solution Explorer), Debug or Release, as shown in Figure 9-2. The drop-down list also has an option to invoke the Configuration Manager.

FIGURE 9-2 The Solution Configuration drop-down list box on the Standard toolbar

You can also use the Property Pages dialog box of a project to determine its configuration and other common properties. To invoke the Property Pages dialog box, shown in Figure 9-3, right-click on the respective project in the Solution Explorer and choose Properties.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

273

 

 

 

 

 

FIGURE 9-3 The Property Pages dialog box of the project Debug1

The Configuration drop-down list box at the top of the dialog box displays the type of configuration of the active project, and the Platform drop-down list box displays the platform (Win32 or .NET) on which the active project is being built. By clicking on the Configuration Manager button, you can invoke the Configuration Manager. You can have different project configurations based on the different permutations of the kinds of build and platform possible.

NOTE

Visual Studio .NET, in its effort to ease your task of development, provides two containers: solutions and projects. A solution is a collection of related projects and other items that are to be built into the application. A project is a collection of files that, when compiled and executed, will produce the required result. All header files, CPP files, and resource files are part of a project. The Solution Explorer, which is part of the IDE, encompasses the solution and its constituent projects.

Having looked at the types of project configurations, Debug and Release, you will now proceed to learn about the debugging tools available as part of Visual Studio

274 Project 1 DATABASE PROGRAMMING USING VC++.NET

.NET. The following are the different debugging tools available in Visual Studio

.NET. These are the different debugging windows of the Visual Studio .NET.

Task List

Breakpoints

Autos

Locals

Watch

QuickWatch

Call Stack

This

Immediate (Command window)

Threads

Modules

Memory

Registers

Disassembly

The Visual Studio .NET supports the same tools as supported in Visual Studio 6.0, such as the various debugging windows (Watch, Locals, and so on). To begin with, in the following section, you will learn about the Task List. Later, you will learn to use some of the predominantly used debugging windows to debug your program.

Task List

The Task List is one of the new features of Visual Studio .NET and its functionality is similar to the Task feature of Microsoft Outlook. You can use the Task List in the following ways:

View compilation errors. The Task List displays the compile-time errors in your application and thereby facilitates you in debugging. After fixing a particular error, you can select the check box against the entry in the Task List to strike out the entry, thereby marking its completion. This feature allows you to easily track the pending errors to be fixed. Figure 9-4 displays the Task List with some sample entries.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

275

 

 

 

 

 

FIGURE 9-4 The Task List with the compilation errors listed

View the TODO comment entries in your code. If you examine any code generated by a wizard, you will notice some TODO comment entries added. The wizard adds these entries to a project so that the developer can use these entries and easily accomplish the required task. You can double-click on an entry in the Task List (see Figure 9-5) and reach the specific line in the file. Similarly, the converse also works. With the Task List window open, try adding a TODO annotation to any of the files. You will notice the entry being created simultaneously in the Task List.

FIGURE 9-5 The Task List window with sample TODO entries

Create user-defined tasks. Besides tracing the errors and the TODO entries, you can also add your own tasks to the Task List. For instance, a team developing a project can share their ideas and suggestions by adding them to the Task List. Once added to the Task List, the ideas and suggestions are available to everyone who has access to the project. Thus, the Task List can act as a common medium of sharing opinions and thoughts.

276 Project 1 DATABASE PROGRAMMING USING VC++.NET

NOTE

Each of the entries in the Task List is assigned a priority, high, low, or normal.

Moving ahead, you will learn to make use of the Task List feature. To begin with, you will learn about some of the categories that can be viewed in the Task List:

All entries. Choose View, Show Tasks, All to view all the tasks in the current file — the one that is open in the Code window.

Comment entries. Choose View, Show Tasks, Comments to view the comment entries, such as the TODO entries, in the current file. Apart from the existing TODO entries, if you add any such new entries with the Comments view active, you can see them being created in the Task List window simultaneously.

You cannot delete the comment entries from the Task List directly. To accomplish this task, you need to remove the corresponding comment entry from the source file.

Build Error entries. Choose View, Show Tasks, Build Errors to view the errors when you build your project. All compilation and linking errors are listed here. Similar to the way you work in the Output window, you can double-click on the entries in the Task List to jump to the code that triggered the errors.

You cannot delete the errors listed in the Task List window. To remove any of the entries, you need to fix the error, and then rebuild the project.

TIP

As stated earlier, each of the entries in the Task List is assigned a priority. All error messages are, by default, assigned high priority while all warnings are assigned normal priority.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

277

 

 

 

 

 

User. The User view helps you to create your own tasks, which may include notes or reminders. For instance, while developing a project, you have to create multiple functions. You might choose to create some of them later. To remind yourself of those pending functions, you can add a task stating, Need to code ‘ABC’ function. After you complete a task, you can check these tasks to indicate that you have completed the task. To create a user-defined task, choose View, Show Tasks, User, and in the Task List window, click on the highlighted text “click here to add a new task.” Then, type the description of the task.

Unlike the comment entries and error entries, you can delete the tasks that you define directly from the Task List by selecting the task and pressing the Delete key.

Shortcut. The Shortcut view allows you to add bookmarks to a specific line of code. Adding shortcuts reduces the time taken to navigate to a particular line of code. To switch to the Shortcut view, choose View, Show Tasks, Shortcut. To add a shortcut, select the line of code, and choose Edit, Bookmarks, Add Task List Shortcut. A shortcut to the specified line is added to the Task List. You can then modify the name of the shortcut and its properties.

To remove a shortcut, select the corresponding entry in the Task List, and press the Delete key. Alternatively, you can click on the entry to place the insertion point in the specific line of code, and choose Edit, Bookmarks, Remove Task List Shortcut.

Checked. This view displays all entries with the check box next to them selected, indicating the completion of the task. This helps you to readily view the list of completed tasks. To activate the Checked view, choose View, Show Tasks, Checked.

Unchecked. The Unchecked view enables you to readily view the list of pending tasks. To activate the Unchecked view, choose View, Show Tasks, Unchecked.

NOTE

Remember that the Task List views will have some entries only if you are working with a specific project in a solution. Otherwise, the Task List window will appear empty.

278 Project 1 DATABASE PROGRAMMING USING VC++.NET

TIP

As an alternative to the View, Show Tasks option, you can choose View, Other Windows, Task List to view the Task List window.

Regardless of the view of the Task List that you are working with, you can perform some common actions, which include setting the priority of the tasks and sorting them. The following list details how to perform these actions:

Setting the priority. To set the priority of any task, click on the leftmost column (the one with the exclamation mark, !, as the column heading) of the task and select the appropriate priority value from the drop-down list.

Sorting the tasks. You can sort the task entries in the Task List based on the category to which they belong, their description, their priority, and so on. To do so, right-click anywhere in the Task List window, select Sort By, and then choose the desired category based on which one you want to sort.

CAUTION

Sometimes, you will not be able to view the tasks added to the Task List. The workaround is to switch to the All view of the Task List, thereby removing the filter. For example, while working in the Comments view of the Task List, you add a userdefined task. After typing the description, when you press Enter, a message box appears stating that you should be in the All view to add the new task.

Working with the Task List Window

You will now browse through some of the views of the Task List window. To do so, first create an MFC application without any database support. The sample application illustrated in the following example is named DebugApp.

1.Choose View, Show Tasks, All. The Task List window appears in the All view with all the tasks available in the file that is currently open. This file contains only TODO entries in it.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

279

 

 

 

 

 

2.Click on the entry TODO: add construction code here. The insertion point is positioned within the view class’s constructor that contains the selected comment entry.

3.Press Enter, and in the next line type “//TODO: Task List feature is great!” Notice the entry appearing simultaneously in the Task List window.

4.Remove // from the line //TODO: Task List feature is great! Build

the project. Notice that the Build Errors view of the Task List is activated and the build errors are listed in the Task List. You can also see a red exclamation mark appearing in the first column against each of the error entries. This indicates that the entries are assigned a high priority. Figure 9-6 shows the Task List in the Build Errors view.

FIGURE 9-6 The Task List window in the Build Errors view

5.Select the check box next to the first error in the Task List window. The task appears in strikethrough, as shown in Figure 9-7, indicating the completion of the task.

FIGURE 9-7 The Task List with an entry in strikethrough

6.Add // to the line TODO: Task List feature is great! Even after you

add the comment symbol, the errors still appear in the Task List. To