Question
Exercise-4: Raising Exceptions Write a function called func that explicitly raises an IndexError exception when called. Then write another function func2 that calls func inside
Exercise-4: Raising Exceptions
Write a function called func that explicitly raises an IndexError exception when called. Then write another function func2 that calls func inside a try/except statement to catch the error. What happens if you change in func to raise KeyError instead of IndexError?
Exercise-5: Raising Exceptions
Change the func function you just wrote to raise an exception you define yourself, called MyError, and pass an extra data item along with the exception. You may identify your exception with a string. Then, extend the try/except of func2 function to catch this exception and its data in addition to IndexError, and print the extra data item.
Sample implementation:
MyError = Sorry, an error occurred
To pass an extra data item along with exception:
raise MyError, OOPS
To catch this error:
except MyError, data:
print 'caught error:', MyError, data
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started