Related Coding Blogs

Practice Top 30 Python Programming Questions on Error Handling and Exception, TechnoVlogs

Practice Top 30 Python Programming Questions on Error Handling and Exception


Q1. Write a program to divide two numbers. Use a try-except block to handle division by zero errors and print "Division by zero is not allowed."  

Input: 
Enter numerator: 10  
Enter denominator: 0  

Expected Output:  
Division by zero is not allowed.

Q2. Write a program to convert a string into an integer. Use a try-except block to handle invalid inputs and print "Invalid input! Please enter a number."  

Input:  
Enter a number: abc  

Expected Output:  
Invalid input! Please enter a number.

Q3. Create a program that raises a ValueError if the input number is negative. Print "Negative numbers are not allowed."  

Input:  
Enter a number: -5  

Expected Output:  
Negative numbers are not allowed. 

Q4. Write a program to read a file. Use the finally clause to ensure the file is closed, even if an error occurs during the reading.  

Input:  
File name: non_existent_file.txt  

Expected Output:  
File not found.  
Closing the file.

Q5. Write a program to add two numbers. Use a try-except block to handle invalid inputs, and an else clause to print the result if no error occurs.  

Input:  
Enter first number: 5  
Enter second number: 7  

Expected Output:  
Result: 12

Q6. Write a program to divide two numbers. Use separate except blocks to handle ZeroDivisionError and ValueError.  

Input:  
Enter numerator: 10  
Enter denominator: abc  

Expected Output:  
Invalid input! Please enter a valid number.

Q7. Create a program that reads a number from the user. Use a nested try-except block to handle cases where the input is invalid or when the result of a computation raises an exception (e.g., dividing by zero).  

Input:  
Enter numerator: abc  
Enter denominator: 5  

Expected Output:  
Invalid input! Please enter a valid number.

Q8. Create a custom exception class NegativeNumberError. Write a program to raise this exception if the user enters a negative number.  

Input:  
Enter a number: -3  

Expected Output:  
Negative numbers are not allowed.

Q9. Write a program to read two integers and divide them. Catch both ValueError and ZeroDivisionError in a single except block.  

Input:  
Enter numerator: 5  
Enter denominator: 0  
 
Expected Output:  
An error occurred: division by zero.

Q10. Write a program that raises an exception with a custom message if a number entered is greater than 100.  

Input:  
Enter a number: 150  

Expected Output:  
ValueError: Number exceeds 100.

Q11. Write a program to read the contents of a file. Use try-except to handle the FileNotFoundError.  

Input:  
Enter file name: missing_file.txt  

Expected Output:  
File not found.

Q12. Write a program that attempts to open and write to a file. Ensure the file is closed properly using the finally block.  

Input:  
File name: test.txt  

Expected Output:  
File written successfully.  
File closed. 

Q13. Write a program that checks whether a number is even or odd. Use else in the try-except block to print "Check completed successfully."  

Input:  
Enter a number: 4  

Expected Output:  
The number is even.  
Check completed successfully.

Q14. Write a program to raise a custom exception TooSmallError if a number entered by the user is less than 10.  

Input:  
Enter a number: 5  

Expected Output:  
TooSmallError: The number is too small.

Q15. Write a program to concatenate two strings. Use a try-except block to handle the case when the inputs are not strings.  

Input:  
Enter first string: Hello  
Enter second string: 123  
 
Expected Output:  
TypeError: Both inputs must be strings.

Q16. Write a function divide_numbers that takes two arguments and divides them. Use a try-except block inside the function to handle ZeroDivisionError.  

Input:  
Enter numerator: 15  
Enter denominator: 0  

Expected Output:  
Error: Division by zero is not allowed.

Q17. Write a program that attempts to call a non-existent method on a string object. Use try-except to catch the AttributeError.  

Input:  
Enter a string: Hello  

Expected Output:  
Error: String object has no attribute 'non_existent_method'.

Q18. Write a program to take two integer inputs and print their sum. Handle exceptions if the inputs are not integers.  

Input:  
Enter first number: 5  
Enter second number: abc  

Expected Output:  
Invalid input! Please enter valid integers.

Q19. Write a program to divide two numbers and use finally to print "Operation complete" whether an exception occurs or not.  

Input:  
Enter numerator: 12  
Enter denominator: 0  

Expected Output:  
Division by zero is not allowed.  
Operation complete.

Q20. Write a program that asks for a number and raises an exception if the number is not between 1 and 10.  

Input: 
Enter a number: 15  

Expected Output:  
ValueError: Number is out of range.

Q21. Create a custom exception class InvalidAgeError that accepts an argument for the invalid age. Raise this exception if the entered age is less than 0 or greater than 150.  

Input:  
Enter age: -5  

Expected Output:  
InvalidAgeError: Invalid age -5 entered.

Q22. Write a program to check if a number is divisible by 3. Use an else clause to print "No errors occurred," and a finally block to print "Check complete."  

Input:  
Enter a number: 9  

Expected Output:  
The number is divisible by 3.  
No errors occurred.  
Check complete.

Q23. Write a program to access an element in a list by index. Use try-except to handle the IndexError when the index is out of range.  

Input:  
Enter a list: [1, 2, 3]  
Enter an index: 5  

Expected Output:  
Error: Index out of range.

Q24. Create a custom exception class CustomError that inherits from Exception. Raise this exception if the entered input is not a palindrome. 

Input:  
Enter a string: hello  

Expected Output:  
CustomError: The input is not a palindrome.

Q25. Write a program with nested try-except blocks to handle multiple potential errors in a mathematical operation. Ensure the finally block executes at the end.  

Input:  
Enter numerator: abc  
Enter denominator: 0  

Expected Output:  
Invalid numerator! Please enter a number.  
Finally block executed.

Q26. Write a program to skip handling a ValueError using the pass statement and print a generic error message instead.  

Input:  
Enter a number: abc  

Expected Output:   
Error: Something went wrong. 

Q27. Write a program that logs exceptions to a file when a user enters an invalid file name.  

Input:  
Enter file name: missing_file.txt  

Expected Output:  
Error logged: File not found.

Q28. Write a program to access a value in a dictionary. Use try-except to handle the KeyError if the key does not exist.  

Input:  
Dictionary: {'a': 1, 'b': 2}  
Key: c  

Expected Output:  
Error: Key does not exist.

Q29. Create a function check_positive that raises a ValueError if the input number is not positive.  

Input:  
Enter a number: -10  

Expected Output:  
ValueError: The number must be positive.

Q30. Write a program that combines try-except, raising exceptions, custom exception classes, and finally/else blocks to validate user input. If the input is valid, print "Input is valid."  

Input:  
Enter a number between 1 and 5: 6  
 
Expected Output:  
ValueError: Number out of range.  
Finally block executed.

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!