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 "
.
string = "problem solving
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.
if 'a' == 'b':
string = 10problems
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.
f = open('file.txt','r')
print(a[0])
, but if a
is defined as an integer, a
can not be indexed and an exception error is shown.
a = 1
print(a[5])
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.
try:
f=open('file.txt','r')
except:
print('file does not exist')
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.
try:
a = 5
print(a[0])
except:
print('variable a is not a list')
except
block does not run.
try:
a = 'Solution'
print(a[0])
except:
print('variable a is not a list')