Custom exception classes in Ruby - Time & Space Complexity
When we create custom exception classes in Ruby, we want to know how this affects the program's speed.
We ask: How does the time to run change as the program grows or uses these exceptions?
Analyze the time complexity of the following code snippet.
class MyError < StandardError
end
def check_value(x)
raise MyError, "Value too small" if x < 10
return x
end
values = [5, 15, 8, 20]
values.each do |v|
begin
puts check_value(v)
rescue MyError => e
puts e.message
end
end
This code defines a custom error and uses it to check values in a list, raising the error if a value is too small.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array with
eachand callingcheck_valuefor each item. - How many times: Once for each element in the array (n times).
As the number of values grows, the program checks each one in turn.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and possible exceptions |
| 100 | 100 checks and possible exceptions |
| 1000 | 1000 checks and possible exceptions |
Pattern observation: The work grows directly with the number of items; doubling items doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of values checked.
[X] Wrong: "Creating a custom exception makes the program slower in a way that changes the overall time complexity."
[OK] Correct: Defining a custom exception class is a one-time setup and does not add repeated work per item; it does not change how the program scales with input size.
Understanding how exceptions affect program speed helps you write clear and efficient error handling, a skill valued in real projects and interviews.
"What if we raised the exception inside a nested loop over the array? How would the time complexity change?"
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
