DEV Community

loading...
Cover image for Understanding Errors in Python

Understanding Errors in Python

Nick Langat
I build intelligent AI/ML models & then integrate them to web APIs. It's not just what I do for a living,it's my purpose!
・3 min read

If you are anything like me,you spend the better part of your day writing code but sometimes the code just will not execute the way you want it to and give you the desired results.This awkward situation is aptly captured by this quote: "Anything that can go wrong will go wrong" ~Murphy's Law.

See, in our capacities as software developers we are still humans and that means from time to time we will make mistakes. Now this mistakes can translate to different errors in our programs. In this article we are going to examine three common errors that Python developers may face in their quest to write programs. Let's get cracking!

ERROR TYPES IN PYTHON

SYNTAX ERRORS

The first type of errors we are looking at are dubbed syntax errors. This errors exist to inform you that there is a rule you are violating and as a result the code will not execute at all. Consider the example below:

valid_voting_age=18
your_age=int(input('Enter your age: '))

if your_age >= valid_voting_age
  print('You can vote')

Enter fullscreen mode Exit fullscreen mode

When that program is run, the code will not execute and an error will be thrown as shown below:
syntax errors
Syntax errors are actually useful since they highlight the exact spot where a violation has occurred and thus you can easily correct the mistake. In our case we forgot to add a colon in the condition if. That is:

valid_voting_age=18
your_age=int(input('Enter your age: '))

if your_age >= valid_voting_age:#new
  print('You can vote')

Enter fullscreen mode Exit fullscreen mode

Let's move on to the second type of errors.

RUNTIME ERRORS

This type of error means that your syntax is correct but it will make the code fail when run. Generally speaking, the Python interpreter understands what you are trying to say but hits an obstacle when following your instructions. This is where no error was detected when the program was parsed and is only realized when a specific line is executed. This makes the program to exit unexpectedly or in other terms, crash.
Consider the following classical example:

num1=55
num2=0
print(num1/num2)

Enter fullscreen mode Exit fullscreen mode

When we run that, we get:

runtime errors

We are trying to divide a number by zero and that crashes our program. Other examples of runtime errors include:
1.Performing an operation on incompatible types.
2.Using an identifier which has not been defined.
3.Accessing a list element, dictionary value or object attribute which does not exist.
4.Accessing a file which does not exist.
To catch runtime errors, you can add in checks to deal with bad inputs and edge cases. In the example above, we may:

num1=55
num2=0
try:
  print(num1/num2)
except ZeroDivisionError:
  print('Cannot divide a number by zero')
Enter fullscreen mode Exit fullscreen mode

LOGICAL ERRORS

These types of errors are a real pain as they are hard to fix.Why? The program runs without crashing but the end result is not what we expect leaving us wondering where hell broke loose. We will not get an error message because no syntax or runtime errors were detected. More often than not, logical errors are as a result of the programmer's carelessness. Examples of things that can lead to logical errors include:
1.Indenting a block to the wrong level
2.Getting operator precedence wrong
3.Making a mistake in a boolean expressions
Among others. Consider the example below:

num1=20
num2=30
if num1>num2:
  print(f'{num1} is less than {num2}')
Enter fullscreen mode Exit fullscreen mode

When we run that code, nothing will be returned to us that we can see. The console will be:

Enter fullscreen mode Exit fullscreen mode

But we expect a print statement right? We will not see it because we used a greater than > operator instead of <.
So because we were not careful enough the algorithm becomes incorrect. Thus we should always exercise care when writing code and proof-read what we have written to ensure nothing has been entered carelessly.

FIGURING OUT WHAT WENT WRONG

OBSERVE THE STACK TRACE

1.Last calls are always on the top
2.Your code is at the bottom
3.Follow line numbers

FIND YOUR MISTAKE

1.Reread your code
2.Check the docs
3.Use sites like StackOverflow, quora, reddit
4.Take a break from the screen
5.Ask someone for help

That's all I have for today. If you enjoyed reading this article then show your love and leave your thoughts down below. Follow me for future updates here and on Twitter

Discussion (0)