Bird
Raised Fist0
Rubyprogramming~20 mins

Custom exception classes in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a custom exception message
What is the output of this Ruby code that defines and raises a custom exception?
Ruby
class MyError < StandardError
  def message
    "Custom error occurred"
  end
end

begin
  raise MyError
rescue MyError => e
  puts e.message
end
ACustom error occurred
BMyError
CStandardError
DRuntimeError
Attempts:
2 left
💡 Hint
Look at the message method inside the custom exception class.
❓ Predict Output
intermediate
2:00remaining
Exception class inheritance check
What will this Ruby code output when checking the inheritance of a custom exception?
Ruby
class MyCustomError < RuntimeError; end

puts MyCustomError < StandardError
puts MyCustomError < RuntimeError
puts MyCustomError < Exception
A
true
true
false
B
false
true
true
C
true
false
true
D
true
true
true
Attempts:
2 left
💡 Hint
Remember that RuntimeError inherits from StandardError, which inherits from Exception.
🔧 Debug
advanced
2:00remaining
Identify the error in custom exception raising
What error does this Ruby code raise when executed?
Ruby
class CustomError < StandardError; end

def test
  raise CustomError, 123
rescue CustomError => e
  puts e.message
end

test
AArgumentError
BNoMethodError
COutputs '123'
DTypeError
Attempts:
2 left
💡 Hint
Check how Ruby converts the second argument of raise to a message string.
❓ Predict Output
advanced
2:00remaining
Custom exception with additional data
What is the output of this Ruby code that adds extra data to a custom exception?
Ruby
class DataError < StandardError
  attr_reader :code
  def initialize(msg, code)
    super(msg)
    @code = code
  end
end

begin
  raise DataError.new("Error happened", 404)
rescue DataError => e
  puts "Message: #{e.message}, Code: #{e.code}"
end
AMessage: Error happened, Code:
BMessage: Error happened, Code: 404
CMessage: , Code: 404
DRuntimeError
Attempts:
2 left
💡 Hint
Look at how the initialize method sets the message and code attributes.
🧠 Conceptual
expert
2:00remaining
Behavior of rescue with multiple custom exceptions
Given these custom exceptions, which rescue clause will catch the raised exception?
Ruby
class ErrorA < StandardError; end
class ErrorB < ErrorA; end

begin
  raise ErrorB
rescue ErrorA
  puts "Caught ErrorA"
rescue StandardError
  puts "Caught StandardError"
end
ACaught ErrorA
BNo output, exception not caught
CCaught StandardError
DRuntimeError
Attempts:
2 left
💡 Hint
Remember that rescue catches exceptions of the specified class or its subclasses.

Practice

(1/5)
1. What is the main purpose of creating a custom exception class in Ruby?
easy
A. To define specific error types for clearer error handling
B. To speed up the program execution
C. To replace all built-in exceptions
D. To automatically fix errors in the code

Solution

  1. Step 1: Understand the role of exceptions

    Exceptions represent errors or unexpected events in a program.
  2. Step 2: Purpose of custom exceptions

    Custom exceptions let you create specific error types to handle different problems clearly.
  3. Final Answer:

    To define specific error types for clearer error handling -> Option A
  4. Quick Check:

    Custom exceptions = clearer error handling [OK]
Hint: Custom exceptions clarify error types for better handling [OK]
Common Mistakes:
  • Thinking custom exceptions speed up code
  • Believing they replace built-in exceptions
  • Assuming they fix errors automatically
2. Which of the following is the correct way to define a custom exception class in Ruby?
easy
A. class MyError < StandardError; end
B. class MyError < Exception; end
C. def MyError; end
D. exception MyError inherits StandardError

Solution

  1. Step 1: Check Ruby syntax for class inheritance

    In Ruby, custom exceptions inherit from StandardError for best practice.
  2. Step 2: Identify correct class definition

    class MyError < StandardError; end correctly defines a class inheriting from StandardError using Ruby syntax.
  3. Final Answer:

    class MyError < StandardError; end -> Option A
  4. Quick Check:

    Custom exception syntax = class MyError < StandardError [OK]
Hint: Custom exceptions inherit StandardError with class syntax [OK]
Common Mistakes:
  • Inheriting directly from Exception (not recommended)
  • Using def instead of class
  • Wrong inheritance syntax
3. What will be the output of this Ruby code?
class MyError < StandardError; end

begin
  raise MyError, "Something went wrong"
rescue MyError => e
  puts e.message
end
medium
A. MyError
B. StandardError
C. No output
D. Something went wrong

Solution

  1. Step 1: Understand raise and rescue

    The code raises a MyError with message "Something went wrong" and rescues it as variable e.
  2. Step 2: Output from exception message

    Inside rescue, puts e.message prints the error message string.
  3. Final Answer:

    Something went wrong -> Option D
  4. Quick Check:

    e.message prints error message string [OK]
Hint: e.message prints the error message string [OK]
Common Mistakes:
  • Printing class name instead of message
  • Expecting no output
  • Confusing exception class with message
4. Identify the error in this Ruby code defining a custom exception:
class MyError
  def initialize(msg)
    super(msg)
  end
end
medium
A. Incorrect method name 'initialize'
B. Missing inheritance from StandardError
C. super should not be called in initialize
D. Class name should be lowercase

Solution

  1. Step 1: Check class inheritance

    The class does not inherit from StandardError or any exception class, so it won't behave as an exception.
  2. Step 2: Confirm other parts are correct

    Method name and super call are correct for passing message to parent class.
  3. Final Answer:

    Missing inheritance from StandardError -> Option B
  4. Quick Check:

    Custom exceptions must inherit StandardError [OK]
Hint: Always inherit StandardError for custom exceptions [OK]
Common Mistakes:
  • Forgetting to inherit StandardError
  • Thinking initialize method name is wrong
  • Believing super call is unnecessary
5. You want to create a custom exception that stores an error code along with the message. Which code correctly implements this?
hard
A. class CodeError < StandardError def initialize(msg) @code = msg end end
B. class CodeError def initialize(msg, code) @code = code super(msg) end end
C. class CodeError < StandardError attr_reader :code def initialize(msg, code) super(msg) @code = code end end
D. class CodeError < Exception def initialize(code) @code = code end end

Solution

  1. Step 1: Check inheritance and attribute

    class CodeError < StandardError attr_reader :code def initialize(msg, code) super(msg) @code = code end end inherits from StandardError and defines attr_reader for code to access it.
  2. Step 2: Verify initialize method

    It calls super(msg) to set the message and stores code in @code correctly.
  3. Final Answer:

    class CodeError < StandardError with attr_reader and initialize(msg, code) -> Option C
  4. Quick Check:

    Custom exception with extra data = class CodeError < StandardError attr_reader :code def initialize(msg, code) super(msg) @code = code end end [OK]
Hint: Use attr_reader and call super(msg) in initialize [OK]
Common Mistakes:
  • Not inheriting from StandardError
  • Calling super after instance variables
  • Missing attr_reader for code
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard