0
0
Rubyprogramming~20 mins

Exception hierarchy in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of exception class inheritance check
What is the output of this Ruby code snippet?
Ruby
puts ZeroDivisionError < StandardError
puts StandardError < Exception
puts Exception < Object
A
true
true
true
B
false
true
true
C
true
false
true
D
true
true
false
Attempts:
2 left
💡 Hint
Remember that ZeroDivisionError inherits from StandardError, which inherits from Exception, which inherits from Object.
Predict Output
intermediate
2:00remaining
Exception rescue specificity
What will be printed when this Ruby code runs?
Ruby
begin
  raise IOError, 'file error'
rescue StandardError
  puts 'rescued StandardError'
rescue IOError
  puts 'rescued IOError'
end
Arescued IOError
Brescued StandardError
Cno output
DRuntimeError
Attempts:
2 left
💡 Hint
Consider the order of rescue clauses and inheritance of IOError.
🔧 Debug
advanced
2:00remaining
Identify the error type raised
What error type will this Ruby code raise when executed?
Ruby
begin
  1 / 0
rescue => e
  puts e.class
end
AArgumentError
BTypeError
CNoMethodError
DZeroDivisionError
Attempts:
2 left
💡 Hint
Dividing by zero in Ruby raises a specific error.
Predict Output
advanced
2:00remaining
Custom exception inheritance check
Given this Ruby code, what is the output?
Ruby
class MyError < StandardError; end
class MySubError < MyError; end

puts MySubError < StandardError
puts MyError < Exception
puts MySubError < Exception
A
true
true
true
B
true
false
true
C
true
true
false
D
false
true
false
Attempts:
2 left
💡 Hint
Trace the inheritance chain from subclasses to their ancestors.
🧠 Conceptual
expert
2:00remaining
Understanding Ruby exception hierarchy root
Which class is the root of all exception classes in Ruby?
AObject
BStandardError
CException
DRuntimeError
Attempts:
2 left
💡 Hint
Think about which class all exceptions inherit from directly or indirectly.