Errors in C#

While compiling your code, you must have come across errors, as one can’t write the whole code perfectly without any mistakes. So, here are some of the types of errors we face while executing our program.

Compilation errors:


When we compile the code, the errors we get when the code doesn’t get compiled are called Compilation errors. When we do typographical errors, type mismatches, misspelling and many other things and try to execute the code, it gives a compilation error.

The syntax errors are caught by the compiler and it throws a compilation error. C# not only tells us there is compilation error but it also tells us where it is giving the error. For example:

In my earlier article I told you about how to create your first program in C#. Let's take the same code. If I write Console.WriteLine(“Hi”) instead of Console.WriteLine(“Hi”); it will give you a compilation error and the error will be error: ;expected . It will also tell you the file name and line number where the error has occurred and when you double click the error, it will take you to the position where the error has occurred.

Logical errors:

Apart from compilation errors, there is one more type of error, logical error. This error occurrs when we write the logic of the code incorrectly. We are the only ones who can find these types of errors. The compiler will find no logical error as the syntax may be right so it will pass the code with no compile errors. For example:

I want to get the perimeter of a square but instead of the right formula i.e. 4*side we write 2*side. The syntax is right but we are not getting the right answer, the reason is incorrect logic.

So, only you can check the logical error by reviewing the code before executing it.

I hope this article has helped you. Please do comment and send your feedback.