Exceptions in Ruby

Tram Ho

  1. Introduce
  2. Handling like ?
  3. Some ways to catch exceptions
  4. Conclusion

1. Introduction

  • Knowing predefined exceptions is one thing, but to use them more effectively in your application, it’s important to understand how to create and customize them according to your own preferences.
  • Essentially, every Ruby exception derives from a built-in Exception class and includes a handful of built-in methods, but the most commonly used exception method is message. This method can be used to retrieve a specific exception message from a raised exception object.
  • As a Ruby programmer must have encountered exceptions, how to handle them? Do you know how to catch exceptions? If you did, do you really understand how it works?
  • In this article I will introduce you to some ways to handle exception in Ruby

2. How to handle?

  • Similar to PHP’s try-catch, Ruby exception handling starts with the begin..rescue block.
  • In summary begin..rescue is a block of code that can be used to handle exceptions raised without interrupting program execution. In other words, you can begin to execute a block of code and rescue any exceptions yet.

3. Some ways to catch exceptions

Use rescue

  • Begin..rescue has basically all the examples of the StandardError class. The types of errors are not included: method errors, type errors, runtime errors, ..
  • To catch the StandardError class exception, simply wrap the specified code in a begin..rescue block:

  • When a StandardError exception is raised in the begin block, an instance of it is passed to the rescue block as variable e (for more information about the structure of the Ruby exception class, see more here ).

  • But what if we want to catch more exceptions than one exception type? Like an if-elsif-else, a block begin..rescue can have multiple rescue..which, when combined with the StandardError class inspection, allowing you to logically adapt to any and all Problems can arise:

  • We can understand better by looking at the list of exceptions below

Use ensure

  • You can also choose to include a ensure clause in your begin..rescue block after the last rescue clause. This ensures that the code in ensure will always be executed, whether an exception is raised or not.
  • So when is this useful? A simple example would be resource management or connection to db, delete temporary files, ..; The code below shows working with a file. Whether or not an exception appears when working with the file, this code ensures that it will always be closed.

  • If there are multiple rescue clauses in the begin..rescue block, this ensure clause acts as a single exit point for the block and allows you to put all of your code in one place, as seen in the code above.
  • Note it’s important to remember that ensure is always executed, and this code does not generate an exception by itself. If the code in the ensure clause generates an exception, any exception raised earlier during the begin..rescue block execution will be ignored and debugging can become very difficult.

Use retry

  • Retry allows you to retry a piece of code within a block
  • Using retry in your begin..rescue block will redirect your program back to the begin command

  • The example below prints the word “Iteration” into the console before issuing an exception.
  • Execute the rescue block, this block will call retry and start over. This results in our program in continuous printing Iteration. Retry keywords allow you to achieve high efficiency using loops. This is useful in situations where you need to try again while repeating the example.

  • result:

4. Conclusion

  • I very much hope that this post has helped you feel confident and caught all kinds of desired exceptions, thanks so much for reading this article.
Share the news now

Source : Viblo