Error and Exception Handling.

"t is common sense to take a method and try it. If it fails,admit it frankly and try another. But above all, try something"

— Franklin Delano Roosevelt

Common


  1. Handling ZeroDivisionError: Write a program that takes two numbers as input and performs division. Use a try-except block to handle the ZeroDivisionError in case the denominator is zero. Display a custom message when this exception occurs.
  2. Handling Multiple Exceptions: Create a program that takes two inputs: a number and a string. Use a try-except block to handle ValueError (if the input cannot be converted to an integer) and TypeError (if trying to perform an invalid operation). Display appropriate messages for each exception.
  3. Using Finally Block: Write a program that reads from a file and prints its content. Use a try-except block to handle FileNotFoundError if the file does not exist. Ensure the file is closed properly using a finally block, whether the exception occurs or not.
  4. Raising Custom Exceptions: Define a custom exception called AgeTooSmallError. Write a program that checks the age input by the user. If the age is below 18, raise the AgeTooSmallError with a custom error message. Use a try-except block to catch and display the exception message.
  5. Handling IndexError: Write a program that asks the user for an index and tries to access that index in a list. Use a try-except block to handle the IndexError if the user enters an invalid index. Print an error message if the index is out of range.
  6. Exception Chaining: Write a program where one exception causes another exception. For instance, inside a try block, raise a ValueError and then, in the except block, raise a TypeError. Use exception chaining to display both exceptions together.
  7. Handling KeyError in Dictionaries: Create a dictionary that stores student names and their scores. Write a program that takes a student's name as input and retrieves their score. Use a try-except block to handle the KeyError if the student’s name is not found in the dictionary, and display a custom error message.
  8. Using Else Block with Exceptions: Write a program that asks the user for a number and converts it to an integer. Use a try-except block to handle ValueError if the input is not a valid number. Include an else block to display a success message when no exception occurs.
  9. Nested Try-Except Blocks: Write a program that takes two numbers and performs division. Inside the try block, use another nested try-except block to handle ZeroDivisionError and ValueError. Ensure the outer block catches any other unexpected exceptions and displays a generic error message.
  10. Re-raising Exceptions: Write a program where a function performs a division operation. Use a try-except block to catch any ZeroDivisionError but then re-raise the exception after displaying a message. The exception should propagate to the caller of the function.

Find it difficult?

Don’t lose heart, don’t be under confident, just be consistent in your preparation and be sure of everything you’ve studied. You can request a class so that we can help you understant this topic.

Feel Confident?

Your first step in learning any new topic is to be aware of your strengths and weaknesses. Once you know this, your self-preparation can be meaningful and result-oriented. Attempt an quiz to get tested.