Errors in a Program

Introduction

Apparently no matter how careful and meticulous you are, there will be errors in your program. When, not if, they occur, you should be able to address the issue. In most cases, both C# and Microsoft Visual Studio can assist you.

The errors your program will encounter can be classified in three categories: runtime, syntax, and logic errors.

Practical LearningPractical Learning: Introducing Errors

  1. Start Microsoft Visual Studio
  2. Create a new Console App named PayrollEvaluation1
  3. Change the contents of the Program.cs document as follows:
    using static System.Console;
    
    int id = default;
    long number = 0L;
    string? firstName = null;
    string? lastName = null;
    double hSal = 0.00;
    double time = 0;
    
    WriteLine("Employee Record");
    WriteLine("------------------------");
    WriteLine("Employee Id:\t{0}", id);
    WriteLine("Employee #:\t{0}", number);
    WriteLine("First Name:\t{0}", firstName);
    WriteLine("Last Name:\t{0}", lastName);
    WriteLine("Hourly Salary:\t{0}", hSal);
    WriteLine("Time Worked:\t{0}", time);
    WriteLine("Weekly Salary:\t{0}", hSal * time);
    WriteLine("===========================");
  4. To execute the application to test it, on the main menu, click Debug and click Start Without Debugging:
    Employee Record
    ------------------------
    Employee Id:    0
    Employee #:     0
    First Name:
    Last Name:
    Hourly Salary:  0
    Time Worked:    0
    Weekly Salary:  0
    ===========================
    
    Press any key to close this window . . .
  5. In the DOS window, press any key to close it and return to your programming environment

Syntax Errors

A syntax error is due to a misuse of the C# language in your code. For example, we already know that, on one hand C# is case-sensitive and, on the other C# has a set of keywords that you should (must) not use to name your variables. The first rule can be difficult to observe if you come from a case-insensitive language like Pascal or Visual Basic. It can also happen through distraction. The second rule is easy to observe if you have used another C-based language such as C++ or Java. Both rules are easy to violate if you write your code using a regular text editor like Notepad.

Fortunately, the built-in Code Editor of Microsoft Visual Studio makes it extremely easy to be aware of syntax errors as soon as they occur:

As you can see, if you create your application in Microsoft Visual Studio, the Code Editor is fully equipped with tools to assist you to detect and correct syntax errors. If you still violate a syntax rule, when you build your project, the compiler would detect the error and point out the line, the section, and the file name where the error occurred. Here is an example:

Operator Misuse

Logic Errors

A logic error occurs when the program (the code) is written fine but the result it produces is not reliable. With a logic error, the Code Editor does not see anything wrong in the document and therefore cannot point out a problem. One of the worse types of logic errors is one that makes a computer crash sometimes, regularly, or unpredictably, while there is nothing obviously wrong in the code.

Logic errors are, or can be, difficult to spot because you will have to know for sure that the result is wrong and why (and sometimes worse, you will have to agree or accept that it is your program that is causing a problem in the computer: a bitter pill to swallow; imagine a user reports that her computer crashes every time she starts the application you created). Because you or the user of your program would know with certainty that the result is questionable, you would have to use some means of correcting it. One of the techniques you can use is referred to as debugging.

Introduction to the Error List Window

Overview

As we have seen so far, a programming language such as Microsoft Visual Studio provides various tools to assist you in writing and managing your code. When it comes to some problems related to code, one of the tools that Microsoft Visual Studio is the Error List window. Most of the times, the Error List is present, usually below the Code Editor. At any time, to display it:

While you are working on your code, the Error List may be minimized. To permanently keep it showing, you can click its AutoHide button.

Introduction to Warnings

When it comes to code, a warning is a problem in your code but that problem may not prevent your program from running. A warning is a signal the compiler presents to you as if asking you whether you can fix it or you think it is not a big deal.

While writing your code, if Micrsofot Visual Studio detects a problem it juges as smal, it creates a warning record and displays it in the Error List window. If you continue typing your code and there is another warning, it gets added to the list of warnings in the Error List. This means that the warning records appear as a table in the Error List. Here is an example:

The Error List Window

Notice that the Error List is equipped with a toolbar. That toolbar is equipped with a button that displays the number of warnings that were found in the code.

Introduction to Errors

An error is a problem that prevents your program from working or from behaving as excepted. While writing your code, if the live parser, which continuously runs while you are writing your code, finds a problem, it creates a list of errors and adds the record of that error to that list. It then displays that list in the Error List. As you keep writing your code, if another violation is detected. It gets added to the list of errors that display in the Error List. Here is an example:

The Error List Window

This time too, notice that the toolbar of the Error List window is equipped with a button that displays the number of errors that were detected.

Errors and Warnings in the Error List

The Error List window is made to display any problem, big or small, that the compiler has detected in your code. Therefore, the Error List is equipped to display both the warnings and the errors. Here is an example:

The Error List Window

Notice that, on the toolbar of the Error List, one button displays the number of errors and another button displays the number of warnings.

Using the Error List Window

Introduction

The Error list window uses a table made of six columns. The first column displays icons, such as the icons for errors and the icons for warnings. The other columns display some titles on their column header. You don't have to use all the columns. To specify what columns to show or hide, right-click anywhere in the body of the Error List and position the mouse on Show Columns:

The Error List Window

To show a column, put a check mark on its menu item. To hide a column, remove its check mark. The list of errors and warnings displays in an incremental order specified by the parser.

The Columns of the Error List

As mentioned already, the Error List uses various columns to show its findings:

Accessing a Problem

To jump to an error or warning from the Error List, locate its entry in the Error List window and double-click it. The caret would be positioned to that Line number, that character or word, and in that File of that Project. If you correct the problem and if the parser concludes that your correction is fine, the error or warning record would be removed automatically from the Error List. You can continue this to correct all problems and, eventually, the Error List would be emptied.

Other Error-Based Windows

The Output window

We just saw that the Error List window can immediately display a list of warnings and errors while you are writing your code. If you are using Microsoft Visual Studio, if is equipped with another window named Output. That window can display warnings and errors that the compiler identifies when you build your code. Unlike the Error List window, the Output window doesn't immediately show a lost of problems in your code. Here is an example:

The Error List Window

The Output Window

When you have built your project, if it doesn't work, the Output window displays a list of problems that prevent that project from executing. Here is an example:

The Output Window

It is important to know that one mistake could be violating more than one rule of the C# language.

The Immediate Window

The Immediate window is a special text editor that can be used to test values, operations (calculations), variables, and functions or methods. To display the Immediate window, on the main menu, you can click Debug -> Windows -> Immediate. The Immediate window appears as a blank object:

Immediate Window

To use it, you must write something in it. What you write depends on what you want to do. An expression you write should start with a question mark but in some cases you can omit that symbol. Because the Immediate window is a text editor, you can copy code from somewhere else and paste it in it.

After typing or pasting the expression, press Enter. The next line would show the result. For example, imagine you want to test an arithmetic operation such as the addition of 248.49 and 57.26, you would type ?248.49 + 57.26 (the empty spaces are optional and you can include as many as you want) and press Enter. Here is an example:

Immediate Window

If you want to test a variable or a function or method, you must first write code that has the variable. That is, before testing a variable, create a function or use the Main() function and declare the variable in it. If you try testing a variable that is not declared, you would receive an error. One way you can test a variable consists of assigning a certain value, probably a wrong value, to it and observe the result. You can start the assignment expression with a question mark but that mark is not necessary.

The Immediate window allows you to test the value that a variable is currently holding. To get this information, in the Immediate window, type the name of the variable preceded by a question mark (you can also just type the name of the variable) and press Enter. If the variable had previously received a value, when you enquire of it in the Immediate window, its current value would show. Another test you can perform on a variable consists of adding a value to it to increase it, or subtracting a value from it.

To test a function or method in the Immediate window, the function or method should return a value. To get the value that a method is currently returning, type its name in the Immediate window and press Enter. You can also precede the name of the function or method with a question mark. In the same way, you can create more elaborate methods and test them in the Immediate window.

If the immediate window starts being crowded, to empty it, you can right-click inside the window and click Clear All.

Debugging Fundaments

Starting and Continuing With Debugging

There are different ways you can start debugging:

In later sections, we will see other ways of starting or proceeding with debugging. In some cases, we will see how you can suspend debugging. When this has happened, to resume debugging:

We will see other ways of continuing with debugging.

Stepping Into Code

As we will see in our lessons, the most basic way to examine code in search of errors is to examine the values of a variable. This is especially useful when the values of a variable keep changing. You do this by running your application and observing a variable. As the primary way to do this operation, the debugger provides an operation referred to as stepping into. This operation consists of executing you code one line (or one statement) at a time and observing what is happening to the variables and other parts of a program.

To execute code one line at a time, while the document that contains it is displaying:

When code is being stepped into, the margin corresponding to the line that is being examined displays a right-pointing yellow arrow:

Debugging - Stepping Into

This lets you know what line is currently considered.

Practical LearningPractical Learning: Stepping Into Code

  1. Click anywhere in the Program.cs document to make sure it has focus.
    On the main menu, click Debug -> Step Into.
    Notice the yellow arrow button on the left of a line of code.
    Notice the black DOS window that comes up. If you are using more than one monitor, position that black window in another monitor:

    Debugging - Stepping Into

  2. On the main menu of Microsoft Visual Studio, click Debug and click Step Into again
  3. Keep clicking Debug -> Step Into. Observe as the focus keeps moving from one line to another in the Code Editor, and keep observing the changes in the DOS window.
    Keep doing that until the line with Weekly Salary: 0 displays

Stopping the Debugging

As we will see in later sections, there are various debugging approaches available in Microsoft Visual Studio. Sometimes you will want to suspend or stop debugging.

To end debugging at any time, on the main menu, click Debug and click Stop Debugging.

Practical LearningPractical Learning: Stopping Debugging

  1. On the main menu, click Debug -> Stop Debugging
  2. Change the document as follows:
    using static System.Console;
    
    int id = 1;
    long number = 597_485;
    string? firstName = "Lynda";
    string? lastName = "Elroy";
    double hSal = 25.73;
    double time = 36.50;
    
    WriteLine("Employee Record");
    WriteLine("------------------------");
    WriteLine("Employee Id:\t{0}", id);
    WriteLine("Employee #:\t{0}", number);
    WriteLine("First Name:\t{0}", firstName);
    WriteLine("Last Name:\t{0}", lastName);
    WriteLine("Hourly Salary:\t{0}", hSal);
    WriteLine("Time Worked:\t{0}", time);
    WriteLine("Weekly Salary:\t{0}", hSal * time);
    WriteLine("===========================");

Stepping Out of Code

We saw that the Step Into operation allows you to examine your code one line at a time. If you keep performing that operation, the effects or results of some lines would become predictable. If that becomes the case, you may want to start the examinations of early lines but then you may want to skeep all the rest of the lines to get to the last line. To let you do this, Microsoft Visual Studio provides an operation named Step Out. To perform the Step Out operation, first start a debugging operation. Then:

Practical LearningPractical Learning: Stepping Out of Code

  1. To start debugging, on the main menu, click Debug -> Step Into.
    Notice that the test starts on the first line
  2. On the main menu of Microsoft Visual Studio, click Debug and click Step Into.
    Do that 8 times
  3. On the main menu of Microsoft Visual Studio, click Debug and click Step Out.
    Notice that the DOS window displays all results
  4. In the DOS window, press any key to close it
  5. To start debugging again, in the Solution Explorer, right-click the name of the project -> Debug -> Step Into New Instance.
    Notice that the test starts on the first line
  6. On the main menu of Microsoft Visual Studio, click Debug and click Step Into.
    Do that 8 times
  7. On the main menu of Microsoft Visual Studio, click Debug and click Step Out
  8. Close your programming environment

Previous Copyright © 2001-2026, FunctionX Thursday 15 January 2026, 08:28 Next