Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions