Most of the time, C# errors are encountered when creating a program. Almost all of the programs you see today suffered at least one error. Errors can be devastating if not fixed immediately. In any programming language, there are 3 types of errors you can encounter.
- Compilation C# Error – This type of error prevents you from compiling and executing your program. Such errors include syntax errors which mean that you violate the rule of the programming language. Another compilation error is using something that does not exist or has not been created yet. Missing files or incomplete information about the project and solution may also cause a compilation error. A compilation error will also occur when the program cannot be run because another process is already using it. Compilation errors are the easiest to fix using Visual Studio tools.
- Logic C# Error – This type of error is when the logic and behavior of the program is not what’s expected. This is the hardest error to fix as you need thorough testing to discover this type of error. An example of a logic error is a program that is supposed to add 2 numbers but subtracts them instead. The cause might be the programmer typed a wrong arithmetic symbol during the coding of the program.
- Exception – This type of error occurs while the program is running thus they are also called runtime errors. A typical event where this error can occur is if the user provides an invalid input to the program that the program cannot process. Another example is when the program accesses a null (nothing) value.
Visual Studio has tools for finding and fixing these types of errors. When typing in the Code Editor, one of the features of these IDE’s is to detect possible errors before you even compile the program. Codes that may cause compilation errors have red squiggly lines below them.
Hover your mouse to these squiggly lines to see a description of the error.
You might encounter green squiggly lines. They indicate warnings in the code but will still allow you to run the program. An example is when you declare a variable and never use it within the program (variables will be discussed later).
Logic errors and exceptions can be handled using the debugging tools and exception handling which will be discussed in a later lesson.
The Error List Window
The C# Error List Window allows you to see all the errors and warnings of the current solution. To open the Error List window, go to View > Error List. By default, the Error List window is located at the bottom area of the IDE. Like the other windows, you can unpin the Error List to enable auto-hide by clicking the pin beside the close button.
You will see messages reported here whenever errors or warnings are detected in your program.
You can see the number of errors and warnings as well as some messages regarding the solution. The Error List consist of multiple columns that provide you in-depth details about the errors.
|Description||The description of the message|
|File||The file where the error is located.|
|Line||The line number of the error within the file.|
|Column||The column or the horizontal position of the error within the line.|
|Project||The project name where the error is located.|
If you run or compile the program with errors, you will be presented with an error window.
Do not check the checkbox located below this window because this window is useful for notifying us that errors exist when we run the code. Clicking Yes will run the previous version of the program that has no errors. Clicking No will halt execution of the program so that you fix the errors presented in the Error List window.
Another nice feature of the Error List window is it mechanically brings you to the a part of your answer that has the error. merely click a mistake listed within the Error List and you may be delivered to the a part of the answer wherever the error exists.
Understanding Errors and Fixing Them
Once all the errors are listed in the Error List window, you need to be able to understand what each error means so you can fix them. The following common errors together with their solution are listed below. The name ‘example’ below will be substituted with a different name depending on the error you encounter.
|; expected||You forgot to add a semicolon at the end of a statement.||Add a ; to fix this error.|
|The name ‘example’ does not exist in the current context.||It simply means that the name ‘example’ was not declared or does not exist in your code.||Erase it or announce it in your code.|
|Just task, call, augmentation, decrement, Associate in Nursingd new protest articulations are often utilised as an announcement.||It means that the code is not a valid C# statement.||Delete the statement.|
|Use of unassigned local variable ‘example’||The variable ‘example’ is not assigned an initial value.||Assign a value to the variable before the statement that calls or use it.|
|The type or namespace name ‘example’ could not be found (are you missing a using directive or an assembly reference?)||It means that there is no type or namespace declared which has a name of ‘example’||You need to create a class or namespace named ‘example’, or add a reference to your program and import with a using statement.|
|‘MyMethod()‘: not all code paths return a value||This means that the method ‘MyMethod()‘ which is supposed to return value, does not return a value on all the paths of the code.||Be sure that all the paths of code will return a value.|
|Cannot implicitly convert type ‘type1’ to ‘type2’||This means that a variable of type2 cannot be converted to type1 without using a cast or conversion methods.||Try using casting or use conversion methods. If it still yields an error, then a conversion for those types is currently not possible.|
Don’t worry if some terms are not clear to you as they will be explained in later chapters. Use the Edit List window’s feature of automatically bringing you to the location where the error exists so you can easily fix it.