w3resource

Python Exception Handling: Exercises, Solutions, and Practice

Python Exception Handling [ 10 exercises with solution ]

1. Write a Python program to handle a ZeroDivisionError exception when dividing a number by zero.

Click me to see the sample solution

2. Write a Python program that prompts the user to input an integer and raises a ValueError exception if the input is not a valid integer.

Click me to see the sample solution

3. Write a Python program that opens a file and handles a FileNotFoundError exception if the file does not exist.

Click me to see the sample solution

4. Write a Python program that prompts the user to input two numbers and raises a TypeError exception if the inputs are not numerical.

Click me to see the sample solution

5. Write a Python program that opens a file and handles a PermissionError exception if there is a permission issue.

Click me to see the sample solution

6. Write a Python program that executes an operation on a list and handles an IndexError exception if the index is out of range.

Click me to see the sample solution

7. Write a Python program that prompts the user to input a number and handles a KeyboardInterrupt exception if the user cancels the input.

Click me to see the sample solution

8. Write a Python program that executes division and handles an ArithmeticError exception if there is an arithmetic error.

Click me to see the sample solution

9. Write a Python program that opens a file and handles a UnicodeDecodeError exception if there is an encoding issue.

Click me to see the sample solution

10. Write a Python program that executes a list operation and handles an AttributeError exception if the attribute does not exist.

Click me to see the sample solution

Python Code Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

[ Want to contribute to Python exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/python-exception-handling-exercises.php