Python

Python Exception Handling

If you are a coding veteran or write production code, you would have certainly come in the contact of those pesky errors. Being able to handle these errors makes you a good programmer, therefore it is very necessary for you to properly debug and handle those errors and bugs. In this tutorial, we will try to have an in-depth analysis of Exception handling in Python.

In case you missed our previous posts you can check them out from these links:

What is Exception Handling?

In easy words, Exceptions are unexpected events that occur during the execution of a program.
An exception might result from a logical error or an unanticipated situation. In Python, exceptions (also known as errors) are objects that are raised (or thrown) by code that encounters an unexpected circumstance.

  • The Python interpreter can also raise an exception should it encounter an unexpected condition, like running out of memory.
  • A raised error may be caught by a surrounding context that “handles” the exception in an appropriate fashion.
  • If uncaught, an exception causes the interpreter to stop executing the program and to report an appropriate message to the console.

In the next sections, we will examine the most common error types in Python, the mechanism for catching and handling errors that have been raised, and the syntax for raising errors from within user-defined blocks of code.


Common Exception classes in Python

ClassDescription
ExceptionA base class for most error types
AttributeErrorRaised by syntax object.fun, if object has no membered name fun
EOFErrorRaised if “end of file” reached for console or file output
IOErrorRaised upon failure of I/O operation (e.g. opening file)
IndexErrorRaised if index to sequence is out of bounds
KeyErrorRaised if nonexistent key requested for set or dictionary
KeyboardInterruptRaised if user types ctrl-C while program is executing
NameErrorRaised if nonexistent identifier used
StopIterationRaised by next(iterator) if no element;
TypeErrorRaised when wrong type of parameter is sent to a function
ValueErrorRaised when the parameter has an invalid value (e.g., sqrt(−5))
ZeroDivisionErrorRaised when any division operator used with 0 as divisor

Let’s see some examples for these exception classes in Python 3.6 Shell.

To open a Python Shell in your PC – Simply type IDLE in the Search bar and select the Python3.x + version of IDLE.

If you don’t have Python IDLE installed in your computer you can go through our Introduction to Python Tutorial to know how to install the same.

Exception Class

This class covers all kinds of exceptions.

>>> i = 'a'
>>> z = int(i)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    z = int(i)
ValueError: invalid literal for int() with base 10: 'a'
>>> try:
 z = int(i)
except Exception:
 print("An Error Occurred")

An Error Occurred

Attribute Error

Raised by syntax object.fun, if an object has no membered name fun

>>> class Car:
 color = 'red'

>>> new_car = Car()
>>> new_car.color
'red'
>>> new_car.name
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    new_car.name
AttributeError: 'Car' object has no attribute 'name'
>>> try:
 print(new_car.name)
except AttributeError:
 print("attribute error occurred")

attribute error occurred

Index Error

This error generally occurs on iterable objects and is raised if index to sequence is out of bounds.

>>> txt = 'pyblog'
>>> for i in range(10):
 print(txt[i])

p
y
b
l
o
g
Traceback (most recent call last):
  File "<pyshell#3>", line 2, in <module>
    print(txt[i])
IndexError: string index out of range
>>> # Or simply
>>> txt[10]
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    txt[10]
IndexError: string index out of range
>>> try:
 print(txt[10])
except IndexError:
 print('Index error occurred')

Index error occurred

Other Error Classes

>>> print(unknown_var)
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    print(unknown_var)
NameError: name 'unknown_var' is not defined
>>> print(1/0)
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    print(1/0)
ZeroDivisionError: division by zero
>>> def divide(i,j):
 return i/j

>>> divide('a','b')
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    divide('a','b')
  File "<pyshell#16>", line 2, in divide
    return i/j
TypeError: unsupported operand type(s) for /: 'str' and 'str'

In the previous examples you saw various errors and the exceptions to handle them. Let’s see more about the working of try/except clause in next section.

Related Post

Process of Exception handling in Python

The try statement works as follows.

  • First, the try clause (the statement(s) between the try and except keywords) is executed.
  • If no exception occurs, the except clause is skipped and execution of the try statement is finished.
  • If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.
  • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.

The first exceptions is handled first.

>>> try:
 int('a')
 try:
  print(1/0)
 except:
  print("I'm handled last")
except:
 print("I'm handled first")

I'm handled first

If the inner try/except clause can’t handle the exceptions they are passed to the outer try/except clause.

>>> try:
 try:
  print(1/0)
 except ValueError:
  print("I can't handle you")
except:
 print("I handled the error")

I handled the error

If no exceptions occurred.

>>> try:
 i = 1
 for j in range(10):
  print(i)
 print('yay no exceptions are there')
except:
 print('error')

1
1
1
1
1
1
1
1
1
1
yay no exceptions are there

Raising an Exception in Python

Now that we have gone through the exception catching part, we will cover exception raising part in this section.

An exception is thrown by executing the raise statement, with an appropriate instance of an exception class as an argument that designates the problem. For example, if a function for computing a square root is sent a negative value as a parameter, it can raise an exception with the command:

>>> def divide(i,j):
 if j == 0:
  raise ValueError('j cannot be zero')
 return i/j

>>> divide(1,0)
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    divide(1,0)
  File "<pyshell#46>", line 3, in divide
    raise ValueError('j cannot be zero')
ValueError: j cannot be zero

How extreme we have to go with error checking?

How much error-checking to perform within a function is a matter of debate. Checking the type and value of each parameter demands additional execution time and, if taken to an extreme, seems counter to the nature of Python. Consider the following built-in sum function which is defined with rigorous error checking.

def sum(values):
    if not isinstance(values, collections.Iterable):
        raise TypeError( 'parameter must be an iterable type' )
    total = 0
    for v in values:
        if not isinstance(v, (int, float)):
            raise TypeError( 'elements must be numeric' )
    total = total+ v
    return total

This is an user defined function for sum which does the same task as in-built sum function.

def sum(values):
    total = 0
    for v in values:
        total = total + v
    return total

Interestingly, this simple implementation performs exactly like Python’s built-in version of the function. Even without the explicit checks, appropriate exceptions are raised naturally by the code.

Fantastic isn’t it!! Well this is why python is such a beautiful language.

You may check out the full list of Python in-built exception handling classes in this link.

That’s it for this tutorial, In the next tutorial we are going to cover the User defined Exception Handling part. So don’t forget to subscribe to our newsletter to stay updated.

Join 1,761 other subscribers
K

Share
Tags: Programming Python Python Basic Tutorial

Recent Posts

  • Programming

Mastering Print Formatting in Python: A Comprehensive Guide

In Python, the print() function is a fundamental tool for displaying output. While printing simple…

8 months ago
  • Programming

Global Variables in Python: Understanding Usage and Best Practices

Python is a versatile programming language known for its simplicity and flexibility. When working on…

8 months ago
  • Programming

Secure Your Documents: Encrypting PDF Files Using Python

PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…

8 months ago
  • Programming

Creating and Modifying PDF Files in Python: A Comprehensive Guide with Code Examples

PDF (Portable Document Format) files are widely used for document exchange due to their consistent…

8 months ago
  • Programming

Boosting Python Performance with Cython: Optimizing Prime Number Detection

Python is a high-level programming language known for its simplicity and ease of use. However,…

8 months ago
  • Programming

Using OOP, Iterator, Generator, and Closure in Python to implement common design patterns

Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…

8 months ago

This website uses cookies.