By invoking Kernel#raise, Ruby really offers you the ability to manually raise exceptions. This gives you the option of selecting the type of exception to raise and even customising the error message. If you don't specify an exception type, Ruby will raise a RuntimeError by default (a subclass of StandardError).
Ruby's technique of dealing with unforeseen events is through exceptions. You've seen exceptions in action if you've ever committed an error in your code, causing your application to crash with a message like SyntaxError or NoMethodError. When you throw an exception in Ruby, everything comes to a halt and your programme begins to shut down.
This section explains how to handle and create exceptions in the best possible way.
To recover from mistakes or release resources, use try/catch/finally blocks.
Without throwing exceptions, handle frequent situations.
Exceptions should be avoided when creating classes.
Instead of returning an error code, throw exceptions.
Handling exceptions in Ruby is a way to make sure that the program will be able to respond to any errors that may occur. This is important because it ensures that the program can still function even if there is an error.
Share a personalized message with your friends.