Understand Python Exception Handling: Try, Except and Finally for Python Beginners – Python Tutorial

By | October 9, 2019

Python exception handling is one of most important part of python tutorial, in this tutorial, we will introduce some basic usage of python exception for beginners. You can learn how to handle python exception.

What is python exception?

Simply, you can see python exception as to an error. For example:

  • 1/0
  • Read a nonexistent file
  • Write some data to a file, however, there is no enough disk space
  • Download some files from internet, however, the network is offline

If you have not run your python script, these operations are correct in python syntax. But if you run your python script, there operations may cause your scritp to stop (1/0) or you can not get your wanted data (Read a nonexistent file), these operations which may cause some errors are python exception. We should handle them.

How to handle python exception?

We can use try except finally statement to handle python exception.

The basic structure of try except finally is:

try:
    #try to run some operations which may cause error/exceptions
except:
    #handle exceptions
finally:
    #do some operation if no any exceptions occur or exceptions have been handled

Explain:

Try statement: In this statement, you can run some operations which may cuase some exceptions, for example: 1/0 or read a nonexistent file. If no any exceptions occur, we will execute finally statement.

except statement: If some exceptions occur in try statement, except statement will be run. You can hanle these exceptions or print some error messages to know what exceptions occur. Then finally statement will be run.

finally statement: This statement is optional, it will be run after try statement (no exceptions occur) or except statement.

To understand the try except finally you can refer to this example:

try:
    print('run try statement')
    x = 1 / 1
except:
    print("run except statement")
finally:
    print('run finally statement')

In try statement, there are not any excetiopns, the execute result is:

run try statement
run finally statement

From the result we can find:

try statement is run, then finally statement is also run. except statement is not run becuase no exceptions occur in try statement.

Look example below:

try:
    print('run try statement')
    x = 1 / 0
except:
    print("run except statement")
finally:
    print('run finally statement')

The result is:

run try statement
run except statement
run finally statement

Becuase an exception occured in try statement (1/0), so except statement is run.

Print the exception message

we can use except Exception as e to show the exception message.

try:
    print('run try statement')
    x = 1 / 0
except Exception as e:
    print("run except statement")
    print(e)
finally:
    print('run finally statement')

The result is:

run try statement
run except statement
division by zero
run finally statement

From the result we can find the exception in try statement is: division by zero.

Leave a Reply