Ruby - raise: Reactivate a Handled Error

Introduction

Here is a simple example of raising a ZeroDivisionError exception and passing on the exception to a method called handleError:


def handleError( e )
   puts( "Error of type: #{e.class}" )
   puts( e )
   puts( "Here is a backtrace: " )
   puts( e.backtrace )
end

begin
    divbyzero
rescue Exception => e
    puts( "A problem just occurred. Please wait..." )
    x = 0
    begin
        raise
    rescue
        handleError( e )
    end
end

Here divbyzero is the name of a method in which the divide-by-zero operation takes place.

handleError method prints more detailed information on the exception:

The backtrace method displays an array of strings showing the filenames and line numbers where the error occurred.

Related Topic