ProgrammingHardWipro · Accenture · Capgemini pattern
What does the following Python 3 code print? class MyErr(Exception): pass try: raise MyErr("custom") except MyErr as e: print("caught", e)
- A. MyErr: custom
- B. custom
- C. caught MyErr
- D. caught custom
Correct answer: D. caught custom
A custom exception subclassing Exception behaves like any other; str(e) returns the message passed to the constructor.
More Python Programming questions
- What does the following Python 3 code print? def f(): for i in range(3): if i == 1: continue print(i, end="") print() f()
- What does the following Python 3 code print? def f(): for i in range(5): if i == 3: break else: print("else-no-break") print(i) f()
- What does the following Python 3 code print? def f(): for i in range(3): pass else: print("else-ran") f()
- What does the following Python 3 code print? while False: print("never") else: print("while-else")
- What does the following Python 3 code print? x = 1 while x < 5: x += 1 if x == 3: break else: print("no-break") print(x)
- What does the following Python 3 code print? print([i for i in range(5) if i != 2][::-1])