Error Handling
โ1. โ ๏ธ Exception Handling
- Imagine filling an online form. If you enter wrong information, the website shows an error message instead of crashing. For example, if age should be a number but you type text, the website asks you to correct it. Exception handling in Python works in a similar way. Exception handling helps us handle errors safely and continue the program when possible. An exception is an error that occurs while a program is running. It is also called a runtime error. If an exception is not handled, the program may stop suddenly.
- If we do not handle an exception, Python stops the program when an error occurs.
Example without exception handling:
num_1 = 10num_2 = 0result = num_1 / num_2print(result)print("Program finished")Output: ZeroDivisionError: division by zero Here, the program crashes at this line:result = num_1 / num_2So this line does not run:print("Program finished")๐ Without exception handling, one runtime error can stop the whole program. - Now, let us handle the error using try-except.
Example with exception handling:
try:num_1 = 10num_2 = 0result = num_1 / num_2print(result)except ZeroDivisionError:print("Error: Cannot divide by zero")print("Program finished")Output: Error: Cannot divide by zero Program finished Here, the program does not crash. Python catches the error and runs the except block. After that, the program continues normally. ๐ Exception handling helps the program continue even when an expected error occurs. - Exception handling is useful because it helps us: โ Prevent the program from crashing suddenly โ Show user-friendly error messages โ Handle invalid input safely โ Continue the program when possible โ Debug errors more easily Example situations: - User enters text instead of a number - File is missing - Division by zero happens - Dictionary key is missing - List index is out of range
- Python commonly uses these blocks for exception handling:
try:Contains code that may raise an exception.except:Handles the exception if it occurs.else:Runs only if no exception occurs.finally:Runs whether an exception occurs or not.
1.1 What is an Exception?
1.2 Program Crash Without Exception Handling
1.3 Same Example With Exception Handling
1.4 Why Handle Exceptions?
1.5 Exception Handling Blocks
2. ๐งช try-except
- Syntax:
try:# Code that may raise an exceptionexcept ExceptionType:# Code to handle the exceptionExample:try:result = 10 / 0except ZeroDivisionError:print("Cannot divide by zero")Output: Cannot divide by zero - ZeroDivisionError occurs when we divide a number by zero.
Example:
try:num_1 = 10num_2 = 0result = num_1 / num_2print(result)except ZeroDivisionError:print("Error: Cannot divide by zero")Output: Error: Cannot divide by zero - ValueError occurs when a function receives the correct data type but an invalid value.
Example without exception handling:
age = int("abc")print(age)Output: ValueError: invalid literal for int() with base 10: 'abc' Here, the program crashes because "abc" cannot be converted into an integer. Example with exception handling:try:age = int("abc")print(age)except ValueError:print("Error: Please enter a valid number")print("Program continues")Output: Error: Please enter a valid number Program continues ๐ With exception handling, the program shows a useful message instead of crashing. - We can use multiple except blocks to handle different errors.
Example:
try:user_input = input("Enter two numbers separated by comma: ")num_1, num_2 = map(float, user_input.split(","))print(f"{num_1} / {num_2}: {num_1 / num_2}")except ZeroDivisionError:print("Error: Cannot divide by zero")except ValueError:print("Error: Invalid input. Please enter two numbers separated by comma.")except Exception as e:print(f"Other Error: {e}")Here: ZeroDivisionError โ handles division by zero. ValueError โ handles invalid number input.Exception as eโ handles any other unexpected error.
2.1 Basic Syntax
2.2 Handling ZeroDivisionError
2.3 Handling ValueError
2.4 Handling Multiple Exceptions
3. โ else and finally
- The
elseblock runs only if no exception occurs. Example:try:num_1 = 10num_2 = 2result = num_1 / num_2except ZeroDivisionError:print("Error: Cannot divide by zero")else:print("Division performed successfully")print(result)Output: Division performed successfully 5.0 ๐elseruns only when the try block does not produce an exception. - The
finallyblock always runs. It runs whether an exception occurs or not. Example:try:num_1 = 10num_2 = 0result = num_1 / num_2except ZeroDivisionError:print("Error: Cannot divide by zero")finally:print("Execution completed")Output: Error: Cannot divide by zero Execution completed ๐finallyis commonly used for cleanup tasks. - Example:
try:user_input = input("Enter two numbers separated by comma: ")num_1, num_2 = map(float, user_input.split(","))print(f"{num_1} / {num_2}: {num_1 / num_2}")except ZeroDivisionError:print("Error: Cannot divide by zero")except ValueError:print("Error: Invalid input. Please enter two numbers separated by comma.")except Exception as e:print(f"Other Error: {e}")else:print("Division performed successfully")finally:print("Execution completed")
3.1 else Block
3.2 finally Block
3.3 Full Exception Handling Example
4. ๐งพ Common Exceptions
| Exception | Cause | Example |
|---|---|---|
| ZeroDivisionError | Division or modulo operation is done by zero. | 10 / 0, 5 % 0 |
| TypeError | An operation is applied to an inappropriate data type. | 5 + "5", len(5) |
| ValueError | A function receives the correct type but an invalid value. | int("abc"), float("xyz") |
| KeyError | A dictionary key is not found. | my_dict = {"a": 1}
print(my_dict["b"]) |
| IndexError | A sequence index is out of range. | my_list = [1, 2, 3]
print(my_list[5]) |
| NameError | A variable is used before it is defined. | print(x) |
| AssertionError | An assert statement fails. | assert 2 + 2 == 5 |
5. ๐งท Assertions
- An assertion is used to check whether a condition is True. If the condition is True, the program continues normally. If the condition is False, Python raises AssertionError.
Assertions are useful for checking assumptions while testing or debugging code.
๐
assertis mostly used for testing and debugging assumptions. ๐ For normal user input validation, raise proper exceptions like ValueError or TypeError. - Syntax:
assert condition, "Error message"Here: condition โ Condition that should be True "Error message" โ Message shown if condition is False - Example:
assert 2 + 2 == 4, "Broken Math"print("First assertion passed")assert 2 + 2 == 5, "Broken Math"print("This line will not run")Output: First assertion passed AssertionError: Broken Math - Example:
def calculate_area_of_rectangle(length, width):assert length > 0, "Length must be a positive number"assert width > 0, "Width must be a positive number"return length * widtharea = calculate_area_of_rectangle(5, 2)print(f"Area of rectangle: {area}")Output: Area of rectangle: 10 Example with invalid value:area = calculate_area_of_rectangle(-5, 2)print(f"Area of rectangle: {area}")Output: AssertionError: Length must be a positive number
5.1 What is Assertion?
5.2 assert Syntax
5.3 Assertion Example
5.4 Assertion with Function
6. ๐จ Raising Exceptions
- Sometimes, we want to create an error ourselves when invalid data is found. The
raisekeyword is used to raise an exception manually. This helps us stop wrong data from moving forward in the program. Example situations: - Age cannot be negative - Price cannot be below zero - Name cannot be empty - Input must be a number - Syntax:
raise ExceptionType("Error message")Example:raise ValueError("Age cannot be negative") - Example:
def check_event_entry(age):if type(age) != int:raise TypeError("Age must be an integer")if age < 0:raise ValueError("Age cannot be negative")if age >= 18:return "Eligible"else:return "Not eligible"print(check_event_entry(18))# Eligibleprint(check_event_entry(15))# Not eligibleprint(check_event_entry(-5))# Raises ValueErrorprint(check_event_entry("twenty"))# Raises TypeError ๐ Run the error examples one at a time. - Example:
def calculate_discount(price, discount):if type(price) not in [int, float]:raise TypeError("Price must be a number")if type(discount) not in [int, float]:raise TypeError("Discount must be a number")if price < 0:raise ValueError("Price cannot be negative")if discount < 0:raise ValueError("Discount cannot be negative")final_price = price - (price * discount / 100)return final_priceprint(calculate_discount(1000, 10))# 900.0print(calculate_discount(-1000, 10))# Raises ValueError ๐ Run the error examples one at a time.
6.1 What is raise?
6.2 raise Syntax
6.3 Raising TypeError and ValueError
6.4 Raising Exceptions in a Function
7. ๐ ๏ธ Error Handling Techniques
- It is better to handle specific exceptions when possible.
Good:
try:result = 10 / 0except ZeroDivisionError:print("Cannot divide by zero")Less specific:try:result = 10 / 0except Exception:print("Something went wrong")๐ Specific exceptions make errors easier to understand and debug. - Do not use broad exception handling everywhere.
Avoid this when possible:
try:# risky codeexcept Exception:passThis hides the error and makes debugging difficult. Better:try:# risky codeexcept ValueError:print("Invalid value")except TypeError:print("Invalid type")
7.1 Use Specific Exceptions
7.2 Avoid Hiding Errors
Practice QuestionsNot started
1. Error Handling
Question 1 of 3
- Write a function that takes a string and returns the number of lowercase characters in it. Handle TypeError if the input is not a string.
- Write a function that takes mass in kg and volume in cubic meter and returns density. Formula:
density = mass / volume. Handle ZeroDivisionError and TypeError where appropriate. - Write a function
get_namethat accepts a dictionary and returns the value of keyname. Handle KeyError if the key is missing. - Write a function
get_third_elementthat accepts a list and returns the third element. Handle IndexError if the list has fewer than three elements.
1.1 Practice error handling tasks:2. Raising Exceptions
Question 2 of 3
- Write a function
can_get_adult_pass(age). Rules: - Raise TypeError if age is not an integer. - Raise ValueError if age is below 0. - Raise ValueError if age is above 100. - Return True if age is 18 or above. - Otherwise return False. - Create a function
get_agethat calculates and returns age when birth year is passed. Rules: - Raise TypeError if birth year is not an integer. - Raise ValueError if birth year is in the future. - Raise ValueError if birth year is too old or unrealistic. - The age returned by the function must be an integer. - Write a function
calculate_rectangle_area(length, width). Rules: - Raise TypeError if length or width is not a number. - Raise ValueError if length or width is less than or equal to 0. - Return area of rectangle.
2.1 Practice tasks raising exceptions:- Write a function
3. Implementation
Question 3 of 3
- Reads the
employee_info.csvfile. - Calculates each employee's age using the previously defined
get_agefunction. - Determines whether each employee is an adult or a minor using
can_get_adult_pass. - Counts total adults and minors.
- Generates a JSON file named
event_logistic.jsonin the following format:{"adult_count": 5, "minor_count": 5}. Use proper exception handling while reading the file, calculating age, and writing JSON output.
3.1 You have employee record details in fileemployee_info.csv. Your company is hosting an event and needs to group employees by age category. Write a program that:- Reads the
