Advance
Python
Programming
Agenda
▰ Types of Errors
▰ Syntax Errors
▰ Runtime Error
▰ Logical Errors
▰ Errors Handling in Python
▰ Try and Except
2
Types of Errors in Python
There are several types of errors that can occur in Python. Each
type indicates a different kind of problem in the code, and
comprehending these error types is crucial in creating effective
Python applications.
The most common types of errors you'll encounter in Python are
syntax errors, runtime errors, logical errors, name errors,
type errors, index errors, and attribute errors. Let's go
through each with examples.
3
Back to main
Syntax Errors:
▰ A syntax error occurs in Python when the interpreter is
unable to parse the code due to the code violating
Python language rules, such as inappropriate
indentation, erroneous keyword usage, or incorrect
operator use.
4
Syntax Errors:
5
Runtime Errors:
▰ In Python, a runtime error occurs when the program is
executing and encounters an unexpected condition that
prevents it from continuing. Runtime errors are also
known as exceptions and can occur for various reasons
such as division by zero, attempting to access an index
that is out of range, or calling a function that does not
exist.
6
1- Name Error <<< Runtime Error
A NameError in Python is raised when the interpreter encounters a variable or
function name that it cannot find in the current scope. This can happen for a variety of
reasons, such as misspelling a variable or function name, using a variable or function
before it is defined, or referencing a variable or function that is outside the current
scope. Here's an example of a NameError in Python:
7
2- Type Error <<< Runtime Error
In Python, a TypeError is raised when an operation or function is applied to an object
of an inappropriate type. This can happen when trying to perform arithmetic or logical
operations on incompatible data types or when passing arguments of the wrong type
to a function. Here's an example of a TypeError in Python:
8
3- Index Error <<< Runtime Error
An IndexError is raised in Python when we try to access an index of a sequence
(such as a string, list, or tuple) that is out of range. This can happen when we try to
access an element that doesn't exist in the sequence or when we try to access an
element at an index that is greater than or equal to the length of the sequence. Here's
an example of an IndexError in Python:
9
that object. This can happen when you misspell the name of an attribute or method or when you try to access a
4. AttributeError <<< Runtime Error
An IndexError is raised in Python when we try to access an index of a sequence
(such as a string, list, or tuple) that is out of range. This can happen when we try to
access an element that doesn't exist in the sequence or when we try to access an
element at an index that is greater than or equal to the length of the sequence. Here's
an example of an IndexError in Python:
10
Logical Errors:
▰ A logical error occurs in Python when the code runs
without any syntax or runtime errors but produces
incorrect results due to flawed logic in the code. These
types of errors are often caused by incorrect
assumptions, an incomplete understanding of the
problem, or the incorrect use of algorithms or formulas.
11
Logical Errors:
▰ Unlike syntax or runtime errors, logical errors can be
challenging to detect and fix because the code runs
without producing any error messages. The results may
seem correct, but the code might produce incorrect
output in certain situations. Here is an example of a
logical error in Python:
▰ def calculate_factorial(n):
12
Try Except Block in Python
Error handling in Python is typically done using try-except blocks,
which allow us to catch and handle exceptions that might
otherwise cause our program to crash or behave unpredictably.
An example of a simple try-except block in Python is:
13
Try Except Block in Python
try:
print(x)
except:
print("An exception occurred, x is not
defined")
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong") 14
Try Except else Block in Python
#The try block does not raise any errors, so
the else block is executed:
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
15