Chaining & Context Managers — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
class ValidationError(Exception):
    def __init__(self, field, message):
        self.field = field
        self.message = message
        super().__init__(f"{field}: {message}")

try:
    raise ValidationError("email", "invalid format")
except ValidationError as e:
    print(e.field)
    print(e.message)
    print(str(e))
Aemail, invalid format, email: invalid format
Bemail: invalid format, email: invalid format, email: invalid format
Cemail, invalid format, ValidationError
DAttributeError: 'ValidationError' object has no attribute 'field'

What is the output of the following code?

javascript
class AppError(Exception):
    def __init__(self, message):
        super().__init__(message)
        self.message = message

try:
    raise AppError("something broke")
except AppError as e:
    print(e.args)
    print(str(e))
ATypeError: Exception.__init__() takes 1 positional argument
B('something broke',), something broke
C(), AppError
D('something broke',), AppError('something broke')

What is the output of the following code?

javascript
try:
    try:
        print("try")
    except ValueError:
        print("except")
    else:
        print("else")
        raise ValueError("from else")
    finally:
        print("finally")
except ValueError as e:
    print("caught by outer:", e)
Atry, else, except, finally
Btry, finally, else, caught by outer: from else
Ctry, else, finally, caught by outer: from else
Dtry, except, else, finally

Sign up free to play

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