Understanding Errors in Programming: Why Bugs Happen and How to Handle Them

Communicating with computers can be frustrating sometimes. They don’t always understand what we’re trying to get them to do, they take everything literally, and they can’t exactly explain what they’re thinking. That means that our programs often won’t work the first time around โ€“ they’ll have errors!


What Is an Error?

An error just means that a program doesn’t work as intended, also called a bug.
Errors are a totally natural part of the programming process. Expert programmers don’t stare at a blank code editor, divine the perfect solution, and end up with a final working program on their first try. And that’s okay! Programming is a cycle of try-fix-try again.


Types of Errors We Might Encounter

Letโ€™s start by understanding the categories of errors we might need to fix.


Syntax Errors

A syntax error means that a program is not valid Python. It doesn’t abide by the official rules of the Python language, just like “eat potato good” does not abide by the official rules of the English language.

When we run a program with a syntax error, the computer immediately reports an error in the console. It doesn’t even try to execute the first line of the program. Because syntax errors deal with the structure of the code, the linter can often catch these errors upfront. Most linter errors we encounter correspond to syntax errors.

Common syntax errors include:

  • Mismatched parentheses
  • Mismatched string quotation marks
  • Invalid indentation

print(“Run me!”)
print(“Syntax errors mean that the code is not valid Python.”)
print(43 + 31 + 589)
print(12.43 + 38.43 +)
print(“Can you find the error?”)


Runtime Errors

Runtime errors are sneakier. The computer only discovers these errors as it’s running the program, hence the name RUN-time. The program is valid Python syntax, but when the computer actually tries to execute a particular instruction, it gets confused.

With runtime errors, the computer executes all the lines of code before the incorrect line. If these lines print values, we see their output appear in the console. The computer only reports an error when it reaches the incorrect line. Then, it terminates the program. It does not execute any remaining lines of code.

print(“Run me!”)
print(“Runtime errors can only be found while the program is running.”)
print(28.32 + 431.89)
print(47 + “82”)
print(“Can you find the error?”)


Logic Errors

Logic errors are a mismatch between what the programmer intended and what the code they wrote actually does. To the computer, these programs seem totally fine. There’s nothing wrong with the Python syntax and it understands all of the instructions. When it runs the program, it executes successfully; there is no error message.

This can make logic errors challenging to spot, because the computer can’t offer any help here. The programmer has to identify why the result they’re getting isn’t quite right.

print(“Run me!”)
print(“Logic errors are when the program result isn’t what you intended.”)
print(“The sum of the numbers from 1 to 10 is:”)
print(1 + 2 + 3 + 4 + 5 + 6 + 7 + 9 + 10)
print(“Can you find the error?”)


Reducing Bugs in Your Code

We can’t eliminate bugs altogether, but we can program defensively to reduce the amount of time we spend dealing with them.


Iterative Development

The most important strategy is to run our code early and often. Think of this like checkpointing.
Imagine I spend 20 minutes writing my whole program, and only then, run it for the first time. If I get an error now, I have no idea which parts of my code work and which don’t.

Instead, we can program iteratively by writing, running, and verifying small chunks of functionality at a time. Then, when we do get an error, we’ll be confident that it was caused by the last change we made.


Use Comments Wisely

In Python, any lines of code that start with the # character are treated as comments. When the computer runs a program, it skips over comments entirely. Comments are for humans, not computers!

We can use comments to leave notes to our future self and other programmers. In particular, it’s helpful to explain the intention behind obscure blocks of code. This helps us verify if the code is doing what it’s supposed to, especially if we come back several days later when we’ve forgotten what we were doing.

In the program above, I add five arbitrary-looking integers. By adding a comment that explains that my intention is to sum multiples of 9, it’s easier for me to later recognize that there’s a logic error here. 34 is not a multiple of 9!

print(“Run me!”)

Computers ignore comments when running the program.

print(“Comments can be used to explain the intention behind blocks of code.”)

Sum the first five multiples of 9.

print(9 + 18 + 27 + 34 + 45)


Try Pair Programming

Pair programming is an active collaboration where one person types and the other person offers suggestions, switching roles every so often. The person typing talks through their thought process as they go, so the second person can validate what they’re doing.

Another person increases our chance of spotting minor errors early. Plus, when we can discuss our assumptions with someone, we can identify any larger logic errors upfront.


Conclusion
Errors are a natural part of coding, not something to fear. By understanding the types of errors and adopting habits like iterative development, thoughtful commenting, and pair programming, we can make the process smoother and more effective.