Looking for Bugs

Programs usually do not work on the first try. Many things can go wrong. Simple typing mistakes and errors in logic cause all kinds of problems, even to experienced programmers. Fortunately, if things go wrong, C# has many tools to help you make things right. For example, if I forget to put the semicolon after the Console.WriteLine(); statement in the Hello World program, the editor places a red squiggle at the end of the line (much like the spell checker in Word). When I try to compile the program, I get an error message in the build menu (see Figure 1.13).

Figure 1.13. The squiggle at the end of the WriteLine() command indicates a missing semicolon. Also note the task list at the bottom of the screen.


When you try to run a program that contains errors, C# informs you that there are build errors and asks whether you want to run anyway. Generally, you say no so that you can fix those errors. Any errors that C# notices are placed in a task list at the bottom of the screen. By clicking an item in this list, you are automatically taken to the appropriate line in the code.

The compiler reports where it noticed the error, which isn’t always where the error is located. Still, it gives you a decent hint about what went wrong.

If a program does not compile correctly, don’t panic. Look at the task list and try to solve each problem in order. Often, solving one problem automatically solves the others.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.197.136