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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!