0
0
Rubyprogramming~10 mins

Exception hierarchy 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 standard error.

Ruby
raise [1], "An error occurred"
Drag options to blanks, or click blank then click option'
AStandardError
BException
CRuntimeError
DSyntaxError
Attempts:
3 left
💡 Hint
Common Mistakes
Using Exception directly instead of StandardError.
Using SyntaxError which is for syntax issues.
2fill in blank
medium

Complete the code to rescue only runtime errors.

Ruby
begin
  # some code
rescue [1] => e
  puts e.message
end
Drag options to blanks, or click blank then click option'
ARuntimeError
BStandardError
CException
DNoMethodError
Attempts:
3 left
💡 Hint
Common Mistakes
Using Exception which rescues all exceptions.
Using StandardError which is more general.
3fill in blank
hard

Fix the error in the code to correctly rescue argument errors.

Ruby
begin
  Integer('abc')
rescue [1] => e
  puts "Error: #{e.message}"
end
Drag options to blanks, or click blank then click option'
AStandardError
BArgumentError
CRuntimeError
DTypeError
Attempts:
3 left
💡 Hint
Common Mistakes
Using RuntimeError which does not catch argument errors.
Using TypeError which is different.
4fill in blank
hard

Fill both blanks to create a custom error class inheriting from the correct superclass.

Ruby
class MyError < [1]
  def message
    "Custom error message"
  end
end

raise [2], "Oops!"
Drag options to blanks, or click blank then click option'
AStandardError
BException
CMyError
DRuntimeError
Attempts:
3 left
💡 Hint
Common Mistakes
Inheriting from Exception which is too broad.
Raising StandardError instead of the custom error.
5fill in blank
hard

Fill all three blanks to correctly check error class and message.

Ruby
begin
  raise [1], "File not found"
rescue [2] => e
  if e.is_a?([3])
    puts e.message
  end
end
Drag options to blanks, or click blank then click option'
ARuntimeError
BStandardError
DArgumentError
Attempts:
3 left
💡 Hint
Common Mistakes
Rescuing ArgumentError which won't catch runtime errors.
Checking for wrong error class in is_a?.