How to Handle Exceptions in Ruby with Rescue
Ruby has a robust error handling mechanism called rescue. The way it works is the keyword rescue specifies an exception handler that will catch and handle any exceptions that are raised in the begin block, the code block preceding the rescue block that may cause an exception. Here’s how it looks: When an exception occurs in the begin code block, Ruby transfers control to the rescue block and executes the code within it.