“try” figuring it out

Jason Drummond
5 min readNov 2, 2020

Working in a fast-paced environment, such as a bootcamp, does not lend itself well to taking the time to properly figure out why our code is not working. There have been many times where my code displays an error and I default to googling the “right” way to run the same bit of code without even glancing at the error message. Getting comfortable with the errors that are being displayed to us will make us better programmers in the long run. First, we need to know a bit about what types of errors can happen while running our code.

There are two main types of errors that we can run into when we try to run a program, Syntax or Logical/Runtime Errors. Whenever python runs into one of these errors in our program it terminates the program and we get a message detailing where the error ocurred and what type of error it is. As someone that is new to programming these messages can be a bit daunting at first but hopefully learning about what errors can happen will make us more comfortable fixing them in the future.

Syntax Errors:

Syntax or Parsing Errors occur when we do not follow the proper structure (syntax) of the python language. These tend to be the most common errors we will get as we are learning. Here is an example of what a Syntax error may look like.

Syntax Error

As we can see from this example, there was a missing colon after the while condition and it was caught once we reached the print statement. We then get an error detailing the file name and the line number where the error occurred as well as an arrow pointing out where the error occurred. These types of errors are generally pretty easy to fix, as long as you know the proper syntax.

Logical/Runtime Errors (Exceptions):

If the syntax of our code is correct we can still get errors when we try to execute it, these types of errors are called Exceptions. These tend to be the hardest to figure out as it may not be easily identifiable where our code is going wrong, especially in a case where you are looping over a list and you get an error at some unknown iteration.

Consider the English instruction, flap your arms and fly to Australia. While the instruction is structurally correct and you can understand its meaning perfectly, it is impossible for you to follow it.

When we run into Exceptions we are still given an error message, the only difference is that we get an arrow pointing to the line where our error is and the type of error that it is. There are many different exceptions and it is strongly recommended to look up what the error means when you find one that you are unfamiliar with. Here are a few of the most common types of Exceptions that you may encounter.

  • ValueError: Appears when a function gets an argument of correct type but improper value.
  • IndexError: Appears when the index of a sequence is out of range.
  • ArithmeticError: Base class for all errors that occur for numeric calculation.
  • OverflowError: Raised when a calculation exceeds maximum limit for a numeric type.
  • KeyError: Raised when the specified key is not found in the dictionary.
  • NameError: Raised when an identifier is not found in the local or global namespace.
  • ValueError: Raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified.
  • RuntimeError: Raised when a generated error does not fall into any category.

How can we handle Exceptions?

We can write programs that highlight our exceptions while also continuing to run the rest of our code. Using a try and except statement allows us to see where a potential error may arise and potentially fix our code much quicker than before. Here is a basic example showing how try/except works.

try/except example

Breaking down the above example the try statement works as follows:

  • The try clause is executed, this is the target bit of code where we expect an error may happen
  • If we get an exception during the try clause everything after the exception is skipped and we go to the except clause
  • If no error occurs the except clause is skipped and we finish the rest of the try statement

This was a very low-level example but clearly shows how we can deal with errors all while not getting those sometimes exhaustive and scary error messages. Here is a more useful example of try/except showings its power while we are trying to iterate over a list.

try/except example 2

Struggle Through Your Errors

While try/except can be a powerful tool to help us see where our code is going wrong, we must first get acquainted with the many different types of errors that we can run into. Then once we get an understanding of these errors we can implement try/except to spit out the errors as our code goes along, hopefully allowing us to fix our code faster. The above examples are just breaking the surface of try/except and I urge you to look into the python documentation for a better understanding of these statements, after all we need to get comfortable doing this.

--

--