0
0
Pythonprogramming~10 mins

Extending built-in exceptions in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a new exception class named MyError that inherits from Exception.

Python
class MyError([1]):
    pass
Drag options to blanks, or click blank then click option'
AError
BException
CBaseException
DValueError
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent class name.
Inheriting from a specific error like ValueError instead of Exception.
2fill in blank
medium

Complete the code to raise the custom exception MyError with the message 'Oops!'.

Python
raise [1]('Oops!')
Drag options to blanks, or click blank then click option'
AException
BError
CMyError
DValueError
Attempts:
3 left
💡 Hint
Common Mistakes
Raising the base Exception instead of the custom one.
Using a built-in error like ValueError by mistake.
3fill in blank
hard

Fix the error in the custom exception class by correctly calling the base class constructor.

Python
class MyError(Exception):
    def __init__(self, message):
        [1](message)
Drag options to blanks, or click blank then click option'
Asuper().__init__
Bself.__init__
CException.__init__
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.__init__ causes infinite recursion.
Using just init without context.
4fill in blank
hard

Fill both blanks to add a custom attribute code to the exception and initialize it.

Python
class MyError(Exception):
    def __init__(self, message, [1]):
        super().__init__(message)
        self.[2] = code
Drag options to blanks, or click blank then click option'
Acode
Bmsg
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and attribute.
Forgetting to assign the attribute.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps error codes to messages for errors with code greater than 100.

Python
errors = {
    [1]: [2] for [3] in error_list if [3].code > 100
}
Drag options to blanks, or click blank then click option'
Aerror.code
Berror.message
Cerror
Derr
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name in the loop.
Swapping keys and values in the dictionary.