Reading Exception Handling
Reading Exception Handling
Objectives
1. Understanding Exceptions
2. Distinguishing Errors from Exceptions
3. Familiarity with Common Python Exceptions
4. Managing Exceptions Effectively
about:blank 1/4
9/28/23, 12:36 PM about:blank
ZeroDivisionError: This error arises when an attempt is made to divide a number by zero. Division by
zero is undefined in mathematics, causing an arithmetic error. For instance:
For example:
result = 10 / 0
# Raises ZeroDivisionError
ValueError: This error occurs when an inappropriate value is used within the code. An example of this is
when trying to convert a non-numeric string to an integer:
For example:
num = int("abc")
# Raises ValueError
FileNotFoundError: This exception is encountered when an attempt is made to access a file that does not
exist.
For example:
with open("nonexistent_file.txt", "r") as file:
content = file.read() # Raises FileNotFoundError
IndexError: An IndexError occurs when an index is used to access an element in a list that is outside the
valid index range.
For example:
my_list = [1, 2, 3]
value = my_list[1] # No IndexError, within range
missing = my_list[5] # Raises IndexError
KeyError: The KeyError arises when an attempt is made to access a non-existent key in a dictionary.
For example:
my_dict = {"name": "Alice", "age": 30}
value = my_dict.get("city") # No KeyError, using .get() method
missing = my_dict["city"] # Raises KeyError
TypeError: The TypeError occurs when an object is used in an incompatible manner. An example
includes trying to concatenate a string and an integer:
For example:
result = "hello" + 5 # Raises TypeError
ImportError: This error is encountered when an attempt is made to import a module that is unavailable.
For example: import non_existent_module
about:blank 2/4
9/28/23, 12:36 PM about:blank
Note: Please remember, the exceptions you will encounter are not limited to just these. There
are many more in Python. However, there is no need to worry. By using the technique
provided below and following the correct syntax, you will be able to handle any exceptions
that come your way.
Handling Exceptions:
Python has a handy tool called try and except that helps us manage exceptions.
Try and Except : You can use the try and except blocks to prevent your program from crashing due to
exceptions.
1. The code that may result in an exception is contained in the try block.
2. If an exception occurs, the code directly jumps to except block.
3. In the except block, you can define how to handle the exception gracefully, like displaying an error
message or taking alternative actions.
4. After the except block, the program continues executing the remaining code.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
1.
2. # using Try- except
3. try:
4. # Attempting to divide 10 by 0
5. result = 10 / 0
6. except ZeroDivisionError:
7. # Handling the ZeroDivisionError and printing an error message
8. print("Error: Cannot divide by zero")
9. # This line will be executed regardless of whether an exception occurred
10. print("outside of try and except block")
Copied!
Next Step
As we finish up this reading, you are ready to move on to the next part where you will practice handling errors.
For better learning, try out different types of data in the lab. This way, you will encounter various errors and
learn how to deal with them effectively. This knowledge will help you write stronger and more reliable code in
the future.
Author(s)
about:blank 3/4
9/28/23, 12:36 PM about:blank
Akansha Yadav
Changelog
Date Version Changed by Change Description
2023-08-11 0.1 Akansha Yadav Initial version created
about:blank 4/4