Complete the code to raise a ValueError with a message.
def check_positive(number): if number <= 0: raise [1]("Number must be positive")
The raise keyword is used to throw an exception. Here, ValueError is the correct exception type to indicate a wrong value.
Complete the code to raise a custom exception with a message.
class MyError(Exception): pass def test(): raise [1]("This is a custom error")
To raise a custom exception, use the class name defined for it. Here, MyError is the custom exception class.
Fix the error in raising an exception with a message.
def check_age(age): if age < 18: raise ValueError[1]"Age must be at least 18"
When raising an exception with a message, the message must be inside parentheses after the exception type.
Fill both blanks to raise a TypeError with a custom message.
def check_string(value): if not isinstance(value, str): raise [1]([2])
The raise statement needs the exception type and the message inside parentheses. Here, TypeError and a descriptive string message are correct.
Fill all three blanks to raise a RuntimeError with a formatted message including a variable.
def check_status(status): if status != 'ok': raise [1](f"Status is [2] but expected [3]")
Use RuntimeError to raise the exception. The message uses an f-string to include the variable status and the expected value 'ok'.