Errors and Exceptions in Python

Errors and Exceptions in Python

In this post, we will discuss Errors and Exceptions in Python.

You are not a human if you have not committed any mistake or error in your life. The code the software developers write might also contain errors, generally referred to as bugs in the programming world. The process of removing these bugs in a program is called debugging. Let us now look at several errors that may occur in a program.

Errors that may occur in a Python program

Listed below are the types of errors that may occur in a Python program.

  • Compile-time errors
  • Runtime errors
  • Logical errors

Compile-time errors

The syntactical errors that may occur in a program when we code it are called compile-time errors. For example, if you would not add a colon for statements such as def, if etc., the compiler detects these errors and prevents you from proceeding further. Usually, in such cases, you might notice an error description in the Python compiler or terminal when a compile-time error occurs.

#Python program to show compile time error
num = 5
if num == 5
  print("Colon is missing")
Output:

  File "C:\Users\Aatmann\Documents\iSapna\Python\Exceptions\compiletime.py", line 3
    if num == 5
               ^
SyntaxError: expected ':'

These errors are likely to occur even if you miss the default indentation in your program.

Runtime errors

Errors that occur when you run the program are called runtime errors. The runtime error usually occurs when the PVM cannot execute the byte code. Some examples of runtime error are TypeError, insufficient memory etc. The Python compiler fails to detect these errors during the compile time.

#Python program to show runtime error
def combine(x, y):
  print(x+y)

combine('Hello',30) #calling combine() and passing arguments
Output:

  File "C:\Users\Aatmann\Documents\iSapna\Python\Exceptions\runtime_error.py", line 5, in <module>
    combine('Hello',30) #calling combine() and passing arguments
  File "C:\Users\Aatmann\Documents\iSapna\Python\Exceptions\runtime_error.py", line 3, in combine
    print(x+y)
TypeError: can only concatenate str (not "int") to str

In the program above, we defined a function called combine() that expects two arguments, ‘x’ and ‘y’. It contains a print statement which adds the values in x and y if they are integers or combines the values if they are string values.

However, we passed one string and one integer value, resulting in TypeError as shown in the output above. Compiler in Python does not check the datatypes during compile time, and the type checking is carried out usually during the runtime.

Logical Errors

The errors that occur due to the flaws in the code or design of the program are called Logical errors. The developer who writes the code is entirely responsible for the occurrence of the logical error in a Python program. These errors are not detected by PVM or Python compiler.

#python program to show the logical errors
def addMarks(marks):
  marks = marks * 11/100
  return marks

#call addMarks() and pass marks
studentMarks = addMarks(700)
print("Marks after adding: ",studentMarks)
Output:

Marks after adding:  77.0

The program’s main objective was to add marks and show the updated marks with an increase. But it ended up showing a lesser score than the original one. As you can see, there were no compile and runtime errors, but we did not get the expected output since the addMarks() method’s logic is incorrect.

Exceptions in Python

Exceptions are runtime errors that a developer can handle. In other words, it means that if a developer anticipates the occurrence of an error during runtime, he can come up with something to deal with the error to provide a smoother user experience. Since exceptions are handled in these cases, this concept is called Exception handling.

We will look at how to handle the exceptions in Python in the next chapter.


Link to all the chapters of our Python tutorial series: Learn Python


Python represents all exceptions as classes. The ones that are available already are called ‘built-in’ exceptions. The base class for all Python exceptions is ‘BaseException‘ class. The subclass ‘Exception‘ is derived from the BaseException class. Please refer to the diagram below to know the crucial exceptions in Python.

Exception Classes in Python
Scroll to Top