Problem Set 03: Exceptions and Context Managers | Python with Problem Solving

Competitive programming is an exciting and challenging field that tests your problem-solving skills and coding abilities. It allows you to tackle a wide range of problems and come up with efficient solutions within a specified time limit. In the world of programming, exceptional circumstances and resource management play crucial roles. That’s why it’s important to understand how to handle exceptions and use context managers effectively. Here are 50 competitive programming problems related to Exceptions and Context Managers that you can solve using Python:

1. Write a program to divide two numbers and handle the ZeroDivisionError.

2. Implement a function that takes a list as input and returns the sum of all the elements. Handle the TypeError if a non-numeric value is encountered.

3. Create a program that reads a file and prints its content. Handle the FileNotFoundError.

4. Write a function that accepts a string and converts it to an integer. Handle the ValueError if the string cannot be converted to an integer.

5. Implement a context manager that prints “Entering the context” when entering and “Exiting the context” when exiting.

6. Create a program that prompts the user to enter a file name and then reads and prints its content. Handle both FileNotFoundError and PermissionError.

7. Write a function that takes a list of numbers as input and returns the product of all the elements. Handle the ValueError if a non-numeric value is encountered.

8. Implement a context manager that measures the time it takes to execute a block of code and prints the elapsed time when exiting.

9. Create a program that reads a JSON file and prints its content. Handle the JSONDecodeError if the file contains invalid JSON.

10. Write a function that accepts a string and converts it to uppercase. Handle the AttributeError if the input is not a string.

11. Implement a context manager that redirects the standard output to a file.

12. Create a program that prompts the user to enter a directory name and then lists all the files in that directory. Handle the NotADirectoryError if the input is not a valid directory.

13. Write a function that accepts a list of integers and returns the average value. Handle the ZeroDivisionError if the input list is empty.

14. Implement a context manager that suppresses a specified exception and logs the error message.

15. Create a program that reads a CSV file and prints its content. Handle the FileNotFoundError and CSVError if the file is not found or contains invalid CSV data.

16. Write a function that accepts a string and checks if it is a palindrome. Handle the TypeError if the input is not a string.

17. Implement a context manager that locks a shared resource and releases it when exiting the context.

18. Create a program that prompts the user to enter a URL and then downloads the content of the webpage. Handle the requests.exceptions.RequestException if an error occurs during the request.

19. Write a function that accepts two integers and calculates their quotient. Handle the ValueError if the input is not a valid integer.

20. Implement a context manager that ensures a file is closed even if an exception occurs.

21. Create a program that reads an XML file and prints its content. Handle the FileNotFoundError and xml.etree.ElementTree.ParseError if the file is not found or contains invalid XML.

22. Write a function that accepts a list of strings and concatenates them. Handle the TypeError if a non-string value is encountered.

23. Implement a context manager that logs the start and end of a block of code.

24. Create a program that prompts the user to enter a database name and then connects to the database. Handle the DatabaseError if the connection fails.

25. Write a function that accepts a string and converts it to a floating-point number. Handle the ValueError if the string cannot be converted to a float.

26. Implement a context manager that opens a file in read mode and returns its content as a list of lines.

27. Create a program that prompts the user to enter a username and password. Handle the AuthenticationError if the credentials are invalid.

28. Write a function that accepts a list of numbers and calculates their sum. Handle the OverflowError if the sum exceeds the maximum limit of an integer.

29. Implement a context manager that sets a specified variable to a certain value when entering the context and restores its original value when exiting.

30. Create a program that reads a binary file and prints its content. Handle the FileNotFoundError and IOError if the file is not found or cannot be read.

31. Write a function that accepts a dictionary and a key, and returns the corresponding value. Handle the KeyError if the key is not found in the dictionary.

32. Implement a context manager that prints the current working directory when entering and exiting.

33. Create a program that prompts the user to enter a file path and then copies the file to a specified destination. Handle the FileNotFoundError and IOError if the file cannot be found or copied.

34. Write a function that accepts a list of strings and sorts them in alphabetical order. Handle the TypeError if a non-string value is encountered.

35. Implement a context manager that captures and logs any unhandled exception that occurs within the context.

36. Create a program that reads a YAML file and prints its content. Handle the FileNotFoundError and yaml.YAMLError if the file is not found or contains invalid YAML.

37. Write a function that accepts two lists of integers and returns their element-wise sum. Handle the ValueError if the input lists have different lengths.

38. Implement a context manager that redirects the standard error output to a file.

39. Create a program that prompts the user to enter a folder path and then deletes all the files in that folder. Handle the FileNotFoundError and PermissionError if the folder or files cannot be accessed.

40. Write a function that accepts a list of numbers and calculates their product. Handle the OverflowError if the product exceeds the maximum limit of an integer.

41. Implement a context manager that measures the memory usage of a block of code and prints the memory usage when exiting.

42. Create a program that prompts the user to enter a file name and then renames the file. Handle the FileNotFoundError and PermissionError if the file cannot be found or renamed.

43. Write a function that accepts a string and converts it to a boolean value. Handle the ValueError if the string cannot be converted to a boolean.

44. Implement a context manager that captures and logs any exception of a specified type that occurs within the context.

45. Create a program that prompts the user to enter a directory path and then counts the number of files in that directory. Handle the NotADirectoryError and PermissionError if the input is not a valid directory or cannot be accessed.

46. Write a function that accepts a list of numbers and calculates their average. Handle the TypeError if a non-numeric value is encountered.

47. Implement a context manager that suppresses multiple specified exceptions and logs the error messages.

48. Create a program that prompts the user to enter a file path and then encrypts the file. Handle the FileNotFoundError and PermissionError if the file cannot be found or encrypted.

49. Write a function that accepts a string and reverses it. Handle the TypeError if the input is not a string.

50. Implement a context manager that ensures a resource is released even if an exception occurs, using the try-finally construct.

These problems should help you practice and strengthen your skills in handling exceptions and using context managers in Python. Happy coding!

Visit our course module on Python with Problem Solving.

Follow us on LinkedIn

60 Days of Python

GitHub

.

Check Out Our Course Modules

Learn without limits from affordable data science courses & Grab your dream job.

Become a Python Developer

Md. Azizul Hakim

Lecturer, Daffodil International University
Bachelor in CSE at KUET, Khulna
Email: azizul@aiquest.org

Data Analysis Specialization

Zarin Hasan

Senior BI Analyst, Apple Gadgets Ltd
Email: zarin@aiquest.org

Become a Big Data Engineer

A.K.M. Alfaz Uddin

Enterprise Data Engineering Lead Engineer at Banglalink Digital Communications Ltd.

Data Science and Machine Learning with Python

Mr. Rashedul Alam Shakil

Founder, aiQuest Intelligence
M. Sc. in Data Science (Major AI)
FAU Erlangen-Nuremberg, Germany

Deep Learning & Generative AI

Md. Asif Iqbal Fahim

Machine Learning Engineer at
IdeaScale Bangladesh Ltd
Kaggle Competition Expert (x2)

Become a Django Developer

Mr. Abu Noman
Software Engineer (Python) at
eAppair Limited