Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reraise (same exception) after catching an exception in Ruby

Tags:

exception

ruby

I am trying to improve my Ruby skills by catching exceptions. I want to know if it is common to reraise the same kind of exception when you have several method calls. So, would the following code make sense? Is it ok to reraise the same kind of exception, or should I not catch it on the process method?

class Logo   def process     begin       @processed_logo = LogoProcessor::create_image(self.src)     rescue CustomException       raise CustomException     end   end end  module LogoProcessor   def self.create_image     raise CustomException if some_condition   end end 
like image 371
Hommer Smith Avatar asked May 20 '14 22:05

Hommer Smith


People also ask

How do you handle exceptions in Ruby?

Ruby also provides a separate class for an exception that is known as an Exception class which contains different types of methods. The code in which an exception is raised, is enclosed between the begin/end block, so you can use a rescue clause to handle this type of exception.

What does rescue mean in Ruby?

A raised exception can be rescued to prevent it from crashing your application once it reaches the top of the call stack. In Ruby, we use the rescue keyword for that. When rescuing an exception in Ruby, you can specify a specific error class that should be rescued from.

Does raising an error stop execution Ruby?

When you raise an exception in Ruby, the world stops and your program starts to shut down. If nothing stops the process, your program will eventually exit with an error message.

How do you raise exceptions in Ruby?

Ruby actually gives you the power to manually raise exceptions yourself by calling Kernel#raise. This allows you to choose what type of exception to raise and even set your own error message. If you do not specify what type of exception to raise, Ruby will default to RuntimeError (a subclass of StandardError ).


2 Answers

Sometimes we just want to know an error happened, without having to actually handle the error.

It is often the case that the one responsible for handling errors is user of the object: the caller. What if we are interested in the error, but don't want to assume that responsibility? We rescue the error, do whatever we need to do and then propagate the signal up the stack as if nothing had happened.

For example, what if we wanted to log the error message and then let the caller deal with it?

begin   this_will_fail! rescue Failure => error   log.error error.message   raise end 

Calling raise without any arguments will raise the last error. In our case, we are re-raising error.

In the example you presented in your question, re-raising the error is simply not necessary. You could simply let it propagate up the stack naturally. The only difference in your example is you're creating a new error object and raising it instead of re-raising the last one.

like image 154
Matheus Moreira Avatar answered Oct 19 '22 20:10

Matheus Moreira


This will raise the same type of error as the original, but you can customize the message.

rescue StandardError => e   raise e.class, "Message: #{e.message}" 
like image 41
FreePender Avatar answered Oct 19 '22 20:10

FreePender