105

What is the difference - technical, philosophical, conceptual, or otherwise - between

raise "foo"

and

raise Exception.new("foo")

?

2 Answers 2

128

Technically, the first raises a RuntimeError with the message set to "foo", and the second raises an Exception with the message set to "foo".

Practically, there is a significant difference between when you would want to use the former and when you want to use the latter.

Simply put, you probably want a RuntimeError not an Exception. A rescue block without an argument will catch RuntimeErrors, but will NOT catch Exceptions. So if you raise an Exception in your code, this code will not catch it:

begin
rescue
end

In order to catch the Exception you will have to do this:

begin
rescue Exception
end

This means that in a sense, an Exception is a "worse" error than a RuntimeError, because you have to do more work to recover from it.

So which you want depends on how your project does its error handling. For instance, in our daemons, the main loop has a blank rescue which will catch RuntimeErrors, report them, and then continue. But in one or two circumstances, we want the daemon to really really die on an error, and in that case we raise an Exception, which goes straight through our "normal error handling code" and out.

And again, if you are writing library code, you probably want a RuntimeError, not an Exception, as users of your library will be surprised if it raises errors that a blank rescue block can't catch, and it will take them a moment to realize why.

Finally, I should say that the RuntimeError is a subclass of the StandardError class, and the actual rule is that although you can raise any type of object, the blank rescue will by default only catch anything that inherits from StandardError. Everything else has to be specific.

3
  • 2
    very informative, thanks. a few things: [1] That last paragraph was the most illuminating, and let me to discover at irb something you didn't mention: RuntimeError < StandardError < Exception [2] therefore, that second block of code will catch both an Exception and a RuntimeError [3] it's interesting/odd that "bare" raise and rescue happen to work with that particular Exception [4] perhaps the rule of thumb is to raise RuntimeError to client code, but raise and rescue one's own custom Exceptions inside one's own code? Jan 26, 2011 at 21:49
  • 1
    [1, 2] Yep. [3] not sure... [4] When I'm coding at my most professional, I tend to create custom error types that inherit from StandardError. It doesn't have to be more complicated than a few lines like class MissingArgumentsError < StandardError; end. Jan 26, 2011 at 21:59
  • Very informative, but under what kind of situations that you will want to throw an Exception rather than run time error if run time error is preferred for writing libraray?
    – Chihung Yu
    May 11, 2015 at 18:18
35

From the offical documentation:

raise   
raise( string )
raise( exception [, string [, array ] ] )

With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil. With a single String argument, it raises a RuntimeError with the string as a message. Otherwise, the first parameter should be the name of an Exception class (or an object that returns an Exception when sent exception). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the rescue clause of begin...end blocks.

raise "Failed to create socket"
raise ArgumentError, "No parameters", caller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.