Custom exception classes help you create your own error types. This makes your program clearer and easier to fix when something goes wrong.
Custom exception classes in Ruby
class MyError < StandardError def initialize(msg = "My custom error happened") super(msg) end end
Custom exceptions usually inherit from StandardError to fit well with Ruby's error handling.
You can add your own methods or data to the custom error class if needed.
StandardError.class MyError < StandardError end
class MyError < StandardError def initialize(msg = "Something went wrong") super(msg) end end
class MyError < StandardError attr_reader :code def initialize(msg = "Error", code = 100) @code = code super(msg) end end
This program defines a custom error MyError. It raises this error if the input value is not positive. The error is caught and its message is printed.
class MyError < StandardError def initialize(msg = "Custom error happened") super(msg) end end def test_error(value) raise MyError, "Value must be positive" if value <= 0 "Value is #{value}" end begin puts test_error(-1) rescue MyError => e puts "Caught a custom error: #{e.message}" end
Always inherit from StandardError unless you have a special reason.
Use raise to trigger your custom error with a message.
Catch your custom errors with rescue YourErrorClass => e to handle them.
Custom exception classes let you create clear, specific errors for your program.
They help separate different error types for better handling.
Use inheritance from StandardError and add messages or extra info as needed.
Practice
Solution
Step 1: Understand the role of exceptions
Exceptions represent errors or unexpected events in a program.Step 2: Purpose of custom exceptions
Custom exceptions let you create specific error types to handle different problems clearly.Final Answer:
To define specific error types for clearer error handling -> Option AQuick Check:
Custom exceptions = clearer error handling [OK]
- Thinking custom exceptions speed up code
- Believing they replace built-in exceptions
- Assuming they fix errors automatically
Solution
Step 1: Check Ruby syntax for class inheritance
In Ruby, custom exceptions inherit from StandardError for best practice.Step 2: Identify correct class definition
class MyError < StandardError; end correctly defines a class inheriting from StandardError using Ruby syntax.Final Answer:
class MyError < StandardError; end -> Option AQuick Check:
Custom exception syntax = class MyError < StandardError [OK]
- Inheriting directly from Exception (not recommended)
- Using def instead of class
- Wrong inheritance syntax
class MyError < StandardError; end begin raise MyError, "Something went wrong" rescue MyError => e puts e.message end
Solution
Step 1: Understand raise and rescue
The code raises a MyError with message "Something went wrong" and rescues it as variable e.Step 2: Output from exception message
Inside rescue, puts e.message prints the error message string.Final Answer:
Something went wrong -> Option DQuick Check:
e.message prints error message string [OK]
- Printing class name instead of message
- Expecting no output
- Confusing exception class with message
class MyError
def initialize(msg)
super(msg)
end
endSolution
Step 1: Check class inheritance
The class does not inherit from StandardError or any exception class, so it won't behave as an exception.Step 2: Confirm other parts are correct
Method name and super call are correct for passing message to parent class.Final Answer:
Missing inheritance from StandardError -> Option BQuick Check:
Custom exceptions must inherit StandardError [OK]
- Forgetting to inherit StandardError
- Thinking initialize method name is wrong
- Believing super call is unnecessary
Solution
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.Step 2: Verify initialize method
It calls super(msg) to set the message and stores code in @code correctly.Final Answer:
class CodeError < StandardError with attr_reader and initialize(msg, code) -> Option CQuick Check:
Custom exception with extra data = class CodeError < StandardError attr_reader :code def initialize(msg, code) super(msg) @code = code end end [OK]
- Not inheriting from StandardError
- Calling super after instance variables
- Missing attr_reader for code
Ready to prove it?
Pass the test and this pattern moves unknown
