w3resource

Python Exception Handling: Exercises, Solutions, and Practice


This resource offers a total of 50 Python Exception Handling problems for practice. It includes 10 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Handle ZeroDivisionError Exception

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

Click me to see the sample solution


2. Validate Integer Input and Raise ValueError

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. Handle FileNotFoundError Exception When Opening a File

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. Validate Numerical Inputs and Raise TypeError

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. Handle PermissionError Exception When Opening a File

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. Handle IndexError in List Operations

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. Handle KeyboardInterrupt Exception When Reading Input

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. Handle ArithmeticError Exception in Division

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. Handle UnicodeDecodeError When Opening a File

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. Handle AttributeError Exception in List Operations

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



Follow us on Facebook and Twitter for latest update.