100 Days Of Python - Day 30
Day 30
Handling Errors and Exceptions
Errors
Errorsare mistakes that occur during the execution of a program.Errorsare caused by invalid code.- Some common errors in Python include:
KeyError- This error occurs when a dictionary does not have a specific key.IndexError- This error occurs when you try to access an index in a list that does not exist.NameError- This error occurs when a variable is not defined.TypeError- This error occurs when an operation or function is applied to the wrong type.ValueError- This error occurs when a built-in operation or function receives an argument that has the right type but an inappropriate value.
Exceptions
Exceptionsare errors that occur during the execution of a program.- They are
handledusingtryandexcept,elseandfinallystatements. Exceptionsare raised when the program encounters an error.
Try, Except, Else, and Finally
try-except-else-finallystatements are used tohandleexceptions.tryis used totesta block of code for errors and is used when we expect an error to occur.exceptis used tohandletheexception. That is, if the try code does not work, the code in the except block will be executed.exceptcan be used to handle a specific error.- It can also be used to handle multiple errors.
- It can be used to catch the error and print a message.
elseis used to execute code if the try code does not raise an exception.finallyis used to execute code, regardless of whether the try code raises an exception or not.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
try:
file = open("a_file.txt") # FileNotFoundError
a_dictionary = {"key": "value"}
print(a_dictionary["key"]) # KeyError
except FileNotFoundError:
file = open("a_file.txt", "w") # Create a file
file.write("Something")
except KeyError as error_message: # Catch the error and print a message
print(f"The key {error_message} does not exist.") # The key 'key' does not exist.
else:
content = file.read()
print(content)
finally:
file.close() # File was closed.
print("File was closed.")# File was closed.
Raising Exceptions
raiseis used to raise an exception.raisecan be used to raise a specific error with atypeand amessage.raisecan be used in situations where we want to raise an error if a certain condition is met. For example, if we want to raise an error if a number is negative.
Example
1
2
3
4
5
6
height = float(input("Height: "))
weight = int(input("Weight: "))
if height > 3:
raise ValueError("Human height should not be over 3 meters.")
bmi = weight / height ** 2
print(bmi)
- In the above example,
- if the height is greater than 3,
- a
ValueErrorwill be raised with the messageHuman height should not be over 3 meters.
IndexError Handling
IndexErroroccurs when you try to access an index in a list that does not exist.IndexErrorcan be handled usingtryandexceptstatements.
Example
1
2
3
4
5
6
7
8
fruits = ["Apple", "Pear", "Orange"]
# IndexError: list index out of range
# print(fruits[3])
# Handle IndexError
try:
print(fruits[3])
except IndexError:
print("Index does not exist.")
- In the above example,
- if the index does not exist,
- the
IndexErrorwill be handled and the messageIndex does not exist.will be printed.
KeyError Handling
KeyErroroccurs when a dictionary does not have a specific key.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
facebook_posts = [
{"Likes": 21, "Comments": 2},
{"Likes": 13, "Comments": 2, "Shares": 1},
{"Likes": 33, "Comments": 8, "Shares": 3},
{"Comments": 4, "Shares": 2},
{"Comments": 1, "Shares": 1},
{"Likes": 19, "Comments": 3},
]
total_likes = 0
# KeyError: 'Likes'
# for post in facebook_posts:
# total_likes = total_likes + post["Likes"]
# print(total_likes)
# Handle KeyError
for post in facebook_posts:
try:
total_likes = total_likes + post["Likes"]
except KeyError:
pass
print(total_likes)
- In the abouve example,
- we are trying to access the key
Likesin the dictionarypost. - In the first loop, the
KeyErrorwill be raised because the keyLikesdoes not exist in the dictionarypost. - In the second loop, the
KeyErroris handled and the loop will continue.
- we are trying to access the key
TypeError Handling
TypeErroroccurs when an operation or function is applied to the wrong type.
- The following example is based on the NATO Phonetic Alphabet project from Day 26.
- If the user enters a name that contains a
number, theTypeErrorwill be raised. In order to handle theTypeError, we can usetryandexceptstatements.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import pandas
data = pandas.read_csv("nato_phonetic_alphabet.csv")
phonetic_dict = {row.letter: row.code for (index, row) in data.iterrows()}
# TypeError: 'int' object is not iterable
# name = input("Enter a name: ").upper()
# nato_list = [phonetic_dict[letter] for letter in name]
# print(nato_list)
# Handle TypeError
try:
name = input("Enter a name: ").upper()
nato_list = [phonetic_dict[letter] for letter in name]
except KeyError:
print("Sorry, only letters in the alphabet please.")
generate_phonetic()
else:
print(nato_list)
generate_phonetic()
# Output
# Enter a name: 123
# Sorry, only letters in the alphabet please.
JSON Data
JSONstands forJavaScript Object Notation.
Writing JSON Data
JSONdata can be written to a file using thejsonmodule which is built into Python.- The
jsonmodule has the following methods:dump()- Used to write data to a file.dumps()- Used to convert a Python object into aJSONstring.
- The
dump()method takes two arguments:- The data to be written.
- The file to write the data to.
- and optionally, the
indentargument which is used to format theJSONdata.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
# Create a dictionary
data = {
"name": "John",
"age": 30,
"married": True,
"divorced": False,
"children": ("Ann", "Billy"),
"pets": None,
"cars": [
{"model": "BMW 230", "mpg": 27.5},
{"model": "Ford Edge", "mpg": 24.1},
],
}
# Convert the dictionary to a JSON string
json_data = json.dumps(data)
# Write the JSON data to a file
with open("data.json", "w") as file:
json.dump(json_data, file, indent=4)
# Output
# {
# "name": "John",
# "age": 30,
# "married": true,
# "divorced": false,
# "children": [
# "Ann",
# "Billy"
# ],
# "pets": null,
# "cars": [
# {
# "model": "BMW 230",
# "mpg": 27.5
# },
# {
# "model": "Ford Edge",
# "mpg": 24.1
# }
# ]
# }
Reading JSON Data
JSONdata can be read from a file also using thejsonmodule which is built into Python.- The
jsonmodule has the following methods:load()- Used to read data from a file.loads()- Used to convert aJSONstring into a Python object.
- The
load()method takes one argument:- The file to read the data from.
- It converts the
JSONdata into a Python dictionary.
Example
1
2
3
4
5
6
7
8
9
10
import json
# Read the JSON data from a file
with open("data.json", "r") as file:
json_data = json.load(file)
# Convert the JSON data into a Python dictionary
data = json.loads(json_data)
print(data["name"]) # John
Updating JSON Data
JSONdata can be updated using thejsonmodule which is built into Python.- The
jsonmodule has the following methods:load()- Used to read data from a file.loads()- Used to convert aJSONstring into a Python object.dump()- Used to write data to a file.dumps()- Used to convert a Python object into aJSONstring.
- Update the
JSONdata as follows:- Read the
JSONdata from the file. - Convert the
JSONdata into a Python dictionary. - Update the Python dictionary.
- Convert the Python dictionary into
JSONdata. - Write the
JSONdata to the file.
- Read the
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import json
# Read the JSON data from a file
with open("data.json", "r") as file:
json_data = json.load(file)
# Convert the JSON data into a Python dictionary
data = json.loads(json_data)
# Update the Python dictionary
data["age"] = 31
# Convert the Python dictionary into JSON data
json_data = json.dumps(data)
# Write the JSON data to a file
with open("data.json", "w") as file:
json.dump(json_data, file, indent=4)
# Output
# {
# "name": "John",
# "age": 31,
# "married": true,
# "divorced": false,
# "children": [
# "Ann",
# "Billy"
# ],
# "pets": null,
# "cars": [
# {
# "model": "BMW 230",
# "mpg": 27.5
# },
# {
# "model": "Ford Edge",
# "mpg": 24.1
# }
# ]
# }
This post is licensed under CC BY 4.0 by the author.