0
0
Rubyprogramming~10 mins

Exception hierarchy in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Exception hierarchy
Start
Raise Exception
Check Exception Type
Is it StandardError?
YesHandle StandardError
Is it Exception?
YesHandle Exception
Unhandled Exception
End
When an exception is raised, Ruby checks its type against the hierarchy from StandardError down to Exception, handling it if matched or leaving unhandled otherwise.
Execution Sample
Ruby
begin
  raise ZeroDivisionError, "divided by zero"
rescue StandardError => e
  puts e.class
end
This code raises a ZeroDivisionError and rescues it as a StandardError, printing the exception class.
Execution Table
StepActionException RaisedException Type CheckedHandled?Output
1Begin block startsNoneNoneNo
2Raise ZeroDivisionErrorZeroDivisionErrorIs it StandardError?Yes
3Rescue block catches exceptionZeroDivisionErrorHandled as StandardErrorYesZeroDivisionError
4End of begin-rescue blockNoneNoneNo
💡 Exception ZeroDivisionError is a subclass of StandardError, so it is handled and output is printed.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
enilZeroDivisionError instanceZeroDivisionError instanceZeroDivisionError instance
Key Moments - 2 Insights
Why does rescuing StandardError catch ZeroDivisionError?
Because ZeroDivisionError is a subclass of StandardError, as shown in execution_table step 2 and 3, so the rescue matches it.
What happens if an exception is not a StandardError subclass?
It is not caught by rescue StandardError and will be unhandled, as the flow shows in the concept_flow after checking StandardError.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, which exception type is raised?
AException
BStandardError
CZeroDivisionError
DRuntimeError
💡 Hint
Check the 'Exception Raised' column at step 2 in the execution_table.
At which step is the exception handled in the execution_table?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Handled?' column with 'Yes' and output printed.
If we raise an exception not inheriting from StandardError, what happens?
AIt is unhandled and causes program to crash
BIt is automatically converted to StandardError
CIt is handled by rescue StandardError
DIt is ignored silently
💡 Hint
Refer to concept_flow where exceptions not matching StandardError are unhandled.
Concept Snapshot
Exception hierarchy in Ruby:
- Exception is the root class
- StandardError is a common subclass
- rescue without args catches StandardError and subclasses
- Specific exceptions inherit from StandardError
- Exceptions outside StandardError are not caught by default rescue
- Use rescue Exception to catch all exceptions (not recommended)
Full Transcript
This visual execution shows how Ruby handles exceptions using its hierarchy. When an exception like ZeroDivisionError is raised, Ruby checks if it is a subclass of StandardError. Since it is, the rescue block catches it and prints the exception class. If an exception is not a StandardError subclass, it remains unhandled and may crash the program. This hierarchy helps organize error handling in Ruby programs.