Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Inheriting from 'object' instead of 'Exception'.
Using 'Error' which is not a built-in base class.
✗ Incorrect
To create a custom exception, you inherit from the built-in Exception class.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Raising the base Exception instead of the custom one.
Using a class name that was not defined.
✗ Incorrect
You raise your custom exception class by calling it with a message.
3fill in blank
hardFix the error in the code to correctly define a custom exception MyError that inherits from Exception.
Python
class MyError(Exception): def __init__(self, message): super().[1](message)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'init' without underscores.
Using a made-up method name like 'constructor'.
✗ Incorrect
The correct method name to call the parent constructor is __init__.
4fill in blank
hardFill both blanks to define a custom exception MyError that stores a message and returns it when converted to string.
Python
class MyError(Exception): def __init__(self, [1]): self.[2] = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and attribute causing errors.
Using undefined variable names.
✗ Incorrect
The parameter and the attribute should have the same name to store the message.
5fill in blank
hardFill both blanks to complete the __str__ method that returns the error message of MyError.
Python
class MyError(Exception): def __init__(self, message): self.message = message def __str__(self): return [1].[2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning undefined variables.
Not using 'self' to access attributes.
✗ Incorrect
The __str__ method returns self.message to show the error message.