Skip to content

Try-Except Statements

Try-Except Statements

Try-except statements are another selection structure in Python. Like if, elif and else statements, a try-except statements select a particular block of code to run based on a condition. Unlike if, elif and else clauses, try-except blocks are not based on logical conditions. Try-except blocks are based upon whether a line or section of code returns an error.

Therefore, before we learn how to use try-except statements, we need to understand two types of errors in Python: syntax errors and exception errors.

Syntax Errors

A syntax error is a type of error in Python that occur when the syntax in a line of code is not valid Python code. Syntax errors include quotes that are not closed and variable names that do not start with a letter.

The line of code below contains a syntax error. The string "problem solving is missing a quotation mark ".

In [1]:
string = "problem solving

  File "<ipython-input-1-4c037f6284bc>", line 1
    string = "problem solving
                             ^
SyntaxError: EOL while scanning string literal

When you encounter syntax errors in Python, the Python interpreter displays SyntaxError and often a cryptic message.

Even if a line of code does not run when a program is executed, syntax errors in Python are not allowed. For instance, a line of code indented after the if-statement if 'a' == 'b': will not be executed. But if the indented line of code contains a syntax error, the Python interpreter still flags the error as a syntax error and does not complete the program.

In [2]:
if 'a' == 'b':
    string = 10problems

  File "<ipython-input-2-532ae1edb2a2>", line 2
    string = 10problems
                      ^
SyntaxError: invalid syntax

Exception Errors

Syntax errors are lines of code that are not valid Python. Another type of error in Python is an exception error. Exception errors result when a valid line of Python code cannot run. Lines of code with exception errors contain valid Python code, but the line of code still cannot be executed.

For example, the statement f = open('file.txt','r') is valid Python code. But if the file file.txt does not exist, Python throws an exception error because f = open('file.txt','r') cannot be executed.

In [3]:
f = open('file.txt','r')

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-cc3c27f5a0c3> in <module>
----> 1 f = open('file.txt','r')

FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'

Another valid line of Python code is print(a[0]), but if a is defined as an integer, a can not be indexed and an exception error is shown.
In [4]:
a = 1
print(a[5])

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-0e1fa8aeb4c3> in <module>
      1 a = 1
----> 2 print(a[5])

TypeError: 'int' object is not subscriptable

Try except statements can be used to try to run sections of Python code that may return an exception error. The general syntax of a try except statement is below:

try:
    <code to try>
except:
    <code to run instead>

For instance, if the file file.txt does not exist, a line of code that tries to open file.txt can be included in a try statement.

In [5]:
try:
    f=open('file.txt','r')
except:
    print('file does not exist')

file does not exist

Similarly, we can wrap the code a = 5 and print(a[0]) in a try block and attempt to run it. If the line a = 5 and print(a[0]) throws an exception error, the code below except runs.
In [6]:
try:
    a = 5
    print(a[0])
except:
    print('variable a is not a list')

variable a is not a list

When the Python code in a try block does run and does not throw an exception error, the code in the except block does not run.
In [7]:
try:
    a = 'Solution'
    print(a[0])
except:
    print('variable a is not a list')

S