0
0
Pythonprogramming~20 mins

Best practices for custom exceptions in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of custom exception handling
What will be the output of this Python code that defines and uses a custom exception?
Python
class MyError(Exception):
    pass

def test():
    try:
        raise MyError("Oops")
    except MyError as e:
        print(f"Caught: {e}")

try:
    test()
except Exception:
    print("General exception caught")
ACaught: Oops
BGeneral exception caught
CNo output
DSyntaxError
Attempts:
2 left
💡 Hint
Look at which exception is raised and which is caught inside the test function.
🧠 Conceptual
intermediate
1:30remaining
Why inherit from Exception for custom exceptions?
Why is it recommended to inherit custom exceptions from the built-in Exception class in Python?
ABecause inheriting from Exception allows the custom exception to be caught by standard except blocks.
BBecause inheriting from Exception disables the traceback.
CBecause inheriting from Exception automatically logs the error.
DBecause inheriting from Exception makes the exception a syntax error.
Attempts:
2 left
💡 Hint
Think about how Python's try-except blocks catch exceptions.
Predict Output
advanced
2:00remaining
Output of custom exception with additional attributes
What is the output of this code that raises a custom exception with extra data?
Python
class ValidationError(Exception):
    def __init__(self, message, code):
        super().__init__(message)
        self.code = code

try:
    raise ValidationError("Invalid input", 400)
except ValidationError as e:
    print(f"Error: {e}, Code: {e.code}")
AError: Invalid input, Code: None
BAttributeError
CError: ValidationError, Code: 400
DError: Invalid input, Code: 400
Attempts:
2 left
💡 Hint
Check how the message and code are passed and accessed.
🔧 Debug
advanced
1:30remaining
Identify the error in this custom exception definition
What error will this code raise when defining a custom exception?
Python
class MyException:
    def __init__(self, message):
        self.message = message

raise MyException("Error happened")
ANameError
BSyntaxError
CTypeError: exceptions must derive from BaseException
DNo error, runs fine
Attempts:
2 left
💡 Hint
Check what Python requires for something to be raised as an exception.
🚀 Application
expert
2:00remaining
How many items in the dictionary of custom exceptions?
Given this code, how many keys are in the dictionary 'errors' after execution?
Python
class ErrorA(Exception): pass
class ErrorB(Exception): pass
class ErrorC(ErrorA): pass

errors = {}
for err in [ErrorA, ErrorB, ErrorC]:
    errors[err.__name__] = err

errors['ErrorA'] = 'Custom message'

A2
B3
C1
D4
Attempts:
2 left
💡 Hint
Consider how dictionary keys are assigned and overwritten.