What if you could catch many errors with just one simple rule instead of dozens of checks?
Why Exception hierarchy in Ruby? - Purpose & Use Cases
Imagine you write a program that can crash for many reasons: file not found, wrong input, or network failure. You try to catch each error separately, but you end up writing many repeated checks everywhere.
Manually checking every possible error is slow and confusing. You might miss some errors or handle them inconsistently. It becomes hard to fix or improve your program because error handling is scattered and messy.
Using an exception hierarchy groups related errors under common categories. You can catch a whole group of errors with one rule or handle specific ones when needed. This makes your code cleaner, easier to read, and simpler to maintain.
if error == 'FileNotFound' handle_file_error elsif error == 'NetworkError' handle_network_error end
begin
# risky code
rescue SystemCallError
handle_io_error
rescue StandardError
handle_general_error
endYou can write smarter error handling that is both flexible and organized, saving time and reducing bugs.
Think of a delivery company sorting packages: all fragile items go to one team, electronics to another. Similarly, exception hierarchy sorts errors so the right handler deals with them efficiently.
Manual error checks get complicated and error-prone.
Exception hierarchy groups errors logically for easier handling.
This leads to cleaner, more maintainable code.
Practice
Which class is the root of the exception hierarchy in Ruby?
Solution
Step 1: Understand Ruby's exception hierarchy
In Ruby, all exceptions inherit from theExceptionclass, which is the root of the hierarchy.Step 2: Identify the root class
Other exception classes likeStandardErrorandRuntimeErrorare subclasses ofException.Final Answer:
Exception -> Option CQuick Check:
Root exception class = Exception [OK]
- Confusing StandardError as root
- Thinking RuntimeError is root
- Choosing specific error classes instead of root
Which of the following is the correct way to rescue only StandardError exceptions in Ruby?
begin # some code rescue ??? puts 'Error caught' end
Solution
Step 1: Understand rescue syntax
Therescuekeyword can specify which exception class to catch by naming it afterrescue.Step 2: Choose the correct exception class
To catch onlyStandardErrorexceptions, writerescue StandardError.Final Answer:
StandardError -> Option AQuick Check:
Rescue specific error = StandardError [OK]
- Using Exception catches too many errors
- Using RuntimeError misses other StandardErrors
- Using IOError catches unrelated errors
What will be the output of this Ruby code?
begin raise IOError, 'File error' rescue StandardError => e puts e.class end
Solution
Step 1: Identify the raised exception
The code raises anIOErrorwith message 'File error'.Step 2: Check rescue clause
The rescue catchesStandardErrorand its subclasses.IOErroris a subclass ofStandardError, so it is caught.Step 3: Output the exception class
The code printse.class, which isIOError.Final Answer:
IOError -> Option DQuick Check:
Raised error class = IOError [OK]
- Assuming rescue prints StandardError
- Confusing Exception with StandardError
- Thinking RuntimeError is printed
Find the error in this Ruby code snippet:
begin raise 'Oops!' rescue IOError puts 'IO error caught' end
Solution
Step 1: Analyze the raised error
Raising with a string'Oops!'creates aRuntimeErrorby default.Step 2: Check rescue clause type
The rescue only catchesIOError, but the raised error isRuntimeError, so it is not caught.Final Answer:
The rescue clause will not catch the raised error -> Option BQuick Check:
Rescue type mismatch causes uncaught error [OK]
- Thinking rescue catches all errors by default
- Believing string raise is invalid syntax
- Assuming rescue syntax is wrong
You want to catch all exceptions except system-exiting ones like SystemExit and Interrupt. Which class should you rescue to catch most errors but allow system exits?
Solution
Step 1: Understand exception classes for system exit
Classes likeSystemExitandInterruptinherit directly fromException, notStandardError.Step 2: Choose rescue class to exclude system exits
RescuingStandardErrorcatches most runtime errors but excludes system-exiting exceptions.Step 3: Confirm other options
RescuingExceptioncatches everything including system exits, which is usually not desired.Final Answer:
StandardError -> Option AQuick Check:
Rescue StandardError to exclude system exits [OK]
- Rescuing Exception catches system exits too
- Choosing RuntimeError misses some errors
- Confusing NoMethodError as base class
Ready to prove it?
Pass the test and this pattern moves unknown
