0
0
Rubyprogramming~10 mins

Raise for throwing errors in Ruby - Interactive Code Practice

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

Complete the code to raise a RuntimeError with a message.

Ruby
raise [1], "Something went wrong!"
Drag options to blanks, or click blank then click option'
Athrow
Bcatch
CRuntimeError
Drescue
Attempts:
3 left
💡 Hint
Common Mistakes
Using throw or catch instead of an error class.
Omitting the error class and only writing raise.
2fill in blank
medium

Complete the code to raise a custom error with a message.

Ruby
class MyError < StandardError; end

raise [1], "Custom error occurred"
Drag options to blanks, or click blank then click option'
AMyError
BException
CError
DStandardError
Attempts:
3 left
💡 Hint
Common Mistakes
Using Exception or Error instead of the custom class.
Not defining the custom error class before raising it.
3fill in blank
hard

Fix the error in the code to correctly raise an ArgumentError with a message.

Ruby
raise ArgumentError [1] "Invalid argument!"
Drag options to blanks, or click blank then click option'
A,
B.
C:
D;
Attempts:
3 left
💡 Hint
Common Mistakes
Using a dot or colon instead of a comma.
Omitting the separator between error class and message.
4fill in blank
hard

Complete the code to raise a ZeroDivisionError with a custom message.

Ruby
raise [1] , "Cannot divide by zero!"
Drag options to blanks, or click blank then click option'
AZeroDivisionError
BArgumentError
C,
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong error class like ArgumentError.
Using a dot instead of a comma to separate the message.
5fill in blank
hard

Fill all three blanks to raise a custom error with a message and rescue it.

Ruby
class [1] < StandardError; end

begin
  raise [2], "Oops!"
rescue [3] => e
  puts e.message
end
Drag options to blanks, or click blank then click option'
ACustomError
DStandardError
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the custom error class.
Rescuing a different error class than the one raised.