C# Errors
Errors
Everyone makes errors when they write code, even experienced programmers. An error in a program is also called a bug.
Errors are a normal part of programming. What matters is that you can find them and fix them.
There are three kinds of errors in C#:
- Compile errors: C# finds them before the program runs. The program does not start.
- Runtime errors: the program starts, but it stops with an error while it runs.
- Logic errors: the program runs, but it gives the wrong answer.
Compile Errors
Before a program runs, the C# compiler reads your code and turns it into a program that the computer can run.
If the compiler finds a mistake, it shows an error message, and the program does not run at all. This is called a compile error.
Here are three common compile errors.
Missing Semicolon
Every statement must end with a semicolon ;. Here, the semicolon is missing after Console.WriteLine(x):
Example
int x = 5;
Console.WriteLine(x)
The compiler shows an error like this:
prog.cs(11,5): error CS1002: ; expected
To fix it, add the semicolon at the end of the line: Console.WriteLine(x);
Unknown Variable
You must create a variable before you can use it. Here, the variable score was never created:
Example
Console.WriteLine(score);
The compiler shows an error like this:
prog.cs(9,25): error CS0103: The name `score' does not exist in the current context
To fix it, create the variable first, for example int score = 50;. Also check the spelling: score and Score are two different names in C#.
Wrong Type
A value must match the type of the variable. Here, "25" is a string (text in double quotes), but age is an int:
Example
int age = "25";
Console.WriteLine(age);
The compiler shows an error like this:
prog.cs(9,17): error CS0029: Cannot implicitly convert type `string' to `int'
"Cannot implicitly convert" means that C# cannot change the string into an int by itself. To fix it, remove the double quotes: int age = 25;
How to Read an Error Message
An error message can look strange at first, but it always has the same parts. Look at the message from the example above:
prog.cs(9,25): error CS0103: The name `score' does not exist in the current context
| Part | Meaning |
|---|---|
prog.cs | The file with the error. The Try it Yourself editor calls your file prog.cs. In Visual Studio, it is often Program.cs. |
(9,25) | Line 9, position 25. This is where C# found the problem. |
CS0103 | The error code. You can search for it online to learn more about the error. |
The name `score' does not exist in the current context | A short text that tells you what is wrong. |
Runtime Errors
A runtime error happens while the program runs. The code compiles, but when a line cannot run, the program stops.
For example, you cannot divide a number by zero:
Example
int x = 10;
int y = 0;
int result = x / y;
Console.WriteLine(result);
The program stops with an error like this:
System.DivideByZeroException: Attempted to divide by zero.
Another common runtime error is an array index that does not exist. This array has 3 items, so the indexes are 0, 1 and 2. There is no index 3:
Example
int[] numbers = {1, 2, 3};
Console.WriteLine(numbers[3]);
The program stops with an error like this:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
Logic Errors
A logic error is the hardest kind to find. The program runs without any error message, but the answer is wrong.
This code should print the average of two scores, 7 and 8. The average is 7.5, but the program prints 7:
Example
int score1 = 7;
int score2 = 8;
double average = (score1 + score2) / 2;
Console.WriteLine(average); // 7
The problem: score1 + score2 and 2 are both integers. When you divide an integer by an integer, C# removes the decimals. So 15 divided by 2 gives 7, not 7.5.
To fix it, divide by 2.0 instead. Then C# keeps the decimals:
Example
int score1 = 7;
int score2 = 8;
double average = (score1 + score2) / 2.0;
Console.WriteLine(average); // 7.5
C# cannot find logic errors for you, because the code is correct C#. It just does not do what you wanted. In the next chapter, Debugging, you will learn how to find errors like this.