0
0
Rubyprogramming~10 mins

Custom exception classes in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom exception classes
Define custom exception class
Raise custom exception
Exception caught?
NoProgram crashes
Yes
Handle exception
Continue
This flow shows how a custom exception class is defined, raised, caught, and handled in Ruby.
Execution Sample
Ruby
class MyError < StandardError; end

def test_error
  raise MyError, "Oops!"
rescue MyError => e
  puts e.message
end

test_error
Defines a custom error, raises it, catches it, and prints the error message.
Execution Table
StepActionEvaluationResult
1Define class MyError < StandardErrorClass createdMyError is ready to use
2Call test_error methodMethod startsExecution enters test_error
3Raise MyError with message 'Oops!'Exception raisedMyError exception thrown
4Rescue MyError exceptionException caughtVariable e holds exception
5puts e.messagePrint messageOutput: Oops!
6Method endsNo more codeReturn nil
7Program endsNo errorsNormal exit
💡 Program ends normally after handling the custom exception.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
enilnilException object (MyError)Exception object (MyError)
Key Moments - 3 Insights
Why do we inherit from StandardError when creating a custom exception?
In Ruby, rescuing StandardError catches most common errors. By inheriting from StandardError, your custom exception works with rescue blocks as shown in step 4.
What happens if we raise the custom exception but do not rescue it?
If not rescued, the program crashes and shows the error message. In the execution_table, step 4 shows catching the exception; without it, the program would stop there.
How do we access the error message inside the rescue block?
The rescue block assigns the exception to variable e (step 4). We use e.message to get the message string, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable e after step 3?
Anil
BString 'Oops!'
CException object (MyError)
DStandardError object
💡 Hint
Check variable_tracker after step 3 to see what e holds.
At which step does the program catch the custom exception?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the execution_table 'Action' column for 'Rescue MyError exception'.
If we remove the rescue block, what would happen at step 4?
AException is caught anyway
BProgram crashes with error
CNothing happens, program continues
DError message prints automatically
💡 Hint
Refer to key_moments about what happens if exception is not rescued.
Concept Snapshot
Define a custom exception class by inheriting StandardError.
Raise it with raise MyError, 'message'.
Use rescue MyError => e to catch it.
Access message with e.message.
Without rescue, program crashes.
Custom exceptions help handle specific errors clearly.
Full Transcript
This example shows how to create a custom exception class in Ruby by inheriting from StandardError. The test_error method raises this custom exception with a message. The rescue block catches the exception and assigns it to variable e. Then, e.message prints the error message. The program continues normally after handling the exception. If the rescue block was missing, the program would crash when the exception is raised. This flow helps you handle errors in your code clearly and safely.