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.