Examples of runtime errors in Python?


In this article, we will look at some examples of runtime errors in Python Programming Language

Runtime Errors

  • A runtime error in a program is one that occurs after the program has been successfully compiled.

  • The Python interpreter will run a program if it is syntactically correct (free of syntax errors). However, if the program encounters a runtime error - a problem that was not detected when the program was parsed and is only revealed when a specific line is executed - it may exit unexpectedly during execution. When a program crashes due to a runtime error, we say it has crashed

Some examples of Python Runtime errors

  • division by zero

  • performing an operation on incompatible types

  • using an identifier that has not been defined

  • accessing a list element, dictionary value, or object attribute which doesn’t exist

  • trying to access a file that doesn’t exist

Let's take a closer look at each of them by using an example

Division by zero

If we divide any number by zero, we get this error.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Take a variable to store the first input number.

  • Take another variable to store the second input number(Here the second number should be 0).

  • Divide the first number by the second number and print the result.

Example

The following program returns the runtime error if we divide a number with zero −

# First Input Number firstNumb = 11 # Second Input Number (Here it is 0) secondNumb = 0 # Dividing the first Number by the second Number print(firstNumb/secondNumb)

Output

On executing, the above program will generate the following output −

Traceback (most recent call last):
  File "main.py", line 6, in <module>
print(firstNumb/secondNumb)
ZeroDivisionError: division by zero

Because the second number is 0 and no number can be divided by 0, we get a runtime error.

Performing an operation on incompatible types

This error occurs when we perform operations like addition, multiplication, and so on on incompatible data types.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Take a string and assign some random value to it and store it in a variable.

  • Take an integer and assign some random integer value to it and store it in another variable.

  • Perform some operations such as addition on the above variables and print it.

Example

The following program returns the runtime error if we perform operation on incompatible data types −

# Input string inputString = "TutorialsPoint" # Input Number inputNumber = 11 # Adding both integer and string values print(inputString + inputNumber)

Output

On executing, the above program will generate the following output −

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    print(inputString + inputNumber)
TypeError: must be str, not int

We can't add an integer to the string data type here, so we get a type error (runtime error).

Using an identifier that has not been defined

This error occurs when we attempt to access an identifier that has not been declared previously.

Example

The following program returns the runtime error if we are using undefined identifier −

# Printing undefined identifier print(tutorialsPoint)

Output

On executing, the above program will generate the following output −

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    print(tutorialsPoint)
NameError: name 'tutorialsPoint' is not defined

Because we did not define the tutorialsPoint identifier and are accessing it by printing it, a runtime error occurs (Name error).

Accessing a list element, dictionary value, or object attribute which doesn’t exist

This runtime error occurs when we attempt to access a non-existent list,string,dictionary element/index.

When we attempt to use an index that does not exist or is too large, it throws an IndexError.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a list and assign some random values to it.

  • Access the list element by index by giving the index that doesn’t exist and printing it.

Example

The following program returns the index error if we access an element out of range −

# input list inputList =[1, 4, 8, 6, 2] # printing the element at index 10 of an input list # throws an IndexError as the index 10 doesn't exist in the input list print("Element at index 10:", inputList[10])

Output

On executing, the above program will generate the following output −

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    print("Element at index 10:", inputList[10])
IndexError: list index out of range

There are 5 elements in the list, and we are attempting to access the 10th index, which does not exist, resulting in an index error.

Trying to access a file that doesn’t exist

If we try to open a file that doesn't exist, it throws a runtime error.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the path of the text file. This is a fixed value. In the example below, this value must be substituted with the file path from your own system.

  • Use the open() function(opens a file and returns a file object as a result) to open the text file in read-only mode by passing the file name, and mode as arguments to it (Here “r” represents read-only mode)

givenFile = open(inputFile,"r")
  • Print it value of the given file.

Example

The following program returns the file not found error if we access an file that doesn’t exist −

# input file path inputFile = "tutorialsPoint.txt" # Opening the given file in read-only mode. givenFile = open(inputFile,"r") # Printing value of given File print(givenFile)

Output

On executing, the above program will generate the following output −

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    givenFile = open(inputFile,"r")
FileNotFoundError: [Errno 2] No such file or directory: 'tutorialsPoint.txt'

Conclusion

We learned about runtime errors in Python in this article. We also used examples to explain the most common Python runtime errors.

Updated on: 31-Oct-2022

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements