DEV Community

Cover image for DotNet Basics: What are the three main types of errors in C#? 🐞
Jarryd
Jarryd

Posted on • Edited on

DotNet Basics: What are the three main types of errors in C#? 🐞

🤔 Question - What was the most annoying programming error you've ever faced? How did you deal with it?

Since we're talking about errors, I thought today would be perfect to cover the fundamentals that every developer encounters.

The reality:

As much as I’d love to believe I can write perfect code without ever seeing an error message…Errors are something every developer deals with whether you're just starting out or have been coding for years.

Over time, I’ve come across all kinds of errors while building applications. Some were quick fixes. Others had me questioning everything.

So, to all the beginners out there navigating the chaos of red squiggly lines and cryptic messages, let’s break down the different types of errors you’re likely to face.

In programming, there are three main types of errors, we’re going to break them down using C#.

1️⃣ Compilation Errors

These happen when your code breaks the rules of the language, your compiler usually catches them before your app even runs.

Example - the compiler throws an error because we forgot a semicolon at the end of the Console.WriteLine("Hello World!") statement or the fact the main method should be spelt "Main"
Image description

🔧 Simple Fix:
Image description


2️⃣ Runtime Errors:

These show up while your program is running and can cause it to crash or behave in unexpected ways (think: dividing by zero or trying to access something that doesn't exist).

Example - We try to access the first element of an empty list
Image description

Error Message:
Image description


3️⃣ Logical Errors:

These are the sneaky ones. Your program runs just fine… but gives you the wrong output because something in your logic is off.

Example - Let’s say we want to join three words—First Name, Middle Name, and Last Name—and print them out as a full name.

We could write the following C# code:
Image description

The code runs without any issues, but the output looks like this:
Image description
Why? Because there's a small logic error. On line 12, we forgot to include a space in the separator:

return string.Join("", words);
Enter fullscreen mode Exit fullscreen mode

to fix this we would change this to:

return string.Join(" ", words);
Enter fullscreen mode Exit fullscreen mode

which gives us the output

Image description

💡 Key Takeaway

Every error is a learning opportunity. The more you encounter and solve them, the better you become at spotting patterns and debugging efficiently. Don't let errors discourage you.

  1. What's the most memorable programming error you've encountered?
  2. How is AI transforming the way you debug code?

Top comments (0)