Try, Except & Finally — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
def risky(n):
    if n == 0:
        raise ValueError("bad n")
    return 10 / n

try:
    risky(0)
except:
    print("caught with bare except")

try:
    risky(2)
except Exception:
    print("this should not print")
else:
    print("no exception, result computed")
Acaught with bare except, no exception, result computed
Bcaught with bare except, this should not print
CValueError: bad n (uncaught)
Dno exception, result computed, caught with bare except

What is the output of the following code?

javascript
try:
    result = 10 // 0
except ZeroDivisionError as e:
    print("ZeroDivisionError:", e)
AZeroDivisionError: cannot divide by zero
BZeroDivisionError: division by zero
CZeroDivisionError: integer division by zero
DZeroDivisionError: 10 // 0

What is the output of the following code?

javascript
try:
    int("abc")
except ValueError as e:
    print(str(e))
    print(repr(e))
Ainvalid literal for int() with base 10: 'abc', invalid literal for int() with base 10: 'abc'
BValueError('abc'), ValueError('abc')
Cinvalid literal for int() with base 10: 'abc', ValueError("invalid literal for int() with base 10: 'abc'")
DValueError: invalid literal for int() with base 10: 'abc', invalid literal for int() with base 10: 'abc'

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.