raise(*args) public

With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil. With a single String argument, 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 object when sent an exception message). 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
Show source
Register or log in to add new notes.
August 27, 2008 - (v1_8_6_287)
3 thanks

Example of raising a custom exception

Create custom exception<br> <pre> class PersonalException < Exception end </pre>

Raise the exception<br> <pre> raise PersonalException.new, “message” </pre>

February 28, 2009
1 thank

rescue

Store exceptions using ‘rescue => var’

begin                              
  x = factorial(-1)             
rescue => ex                       
  puts "#{ex.class}: #{ex.message}"
end                                
February 28, 2009
0 thanks

Exceptions while debugging

If the error wasn’t stored in a variable, you can still see it by looking at the global variable $ERROR_INFO.