Errors

Even the most experienced developers make mistakes, and that is normal. In C#, errors can be of different types: compilation errors, runtime errors, and logical errors.

Compilation errors

These errors occur during program compilation and are related to syntax or type issues. For example, a type mismatch:
Line 1, Char 1

Runtime errors

These errors happen while the program is running, usually due to unexpected circumstances or incorrect data. For example, division by zero:
Line 1, Char 1

Logical errors

These errors may not cause compilation or runtime errors, but they lead to incorrect results.
Line 1, Char 1

How to find and fix errors

  • Read compiler messages: they often explain in detail what went wrong.
  • Use the debugger: you can step through code and track variable values.
  • Logs: it is useful to add console output to track program execution.

Exercises

  1. Run the code and look at the compiler error. What does it mean? Fix the code so that it runs without errors. Hint: you need to change int to string.
Line 1, Char 1
  1. Run the code and look at the runtime error. What does it mean? Fix the code so that it runs without errors.
Line 1, Char 1
  1. There is a logical error in the code. Fix it.
Line 1, Char 1