Table of Contents Previous Next
Logo
Client-Side Slice-to-Ruby Mapping : 26.13 Exception Handling
Copyright © 2003-2009 ZeroC, Inc.

26.13 Exception Handling

Any operation invocation may throw a run-time exception (see Section 26.10) and, if the operation has an exception specification, may also throw user exceptions (see Section 26.9). Suppose we have the following simple interface:
exception Tantrum {
    string reason;
};

interface Child {
    void askToCleanUp() throws Tantrum;
};
Slice exceptions are thrown as Ruby exceptions, so you can simply enclose one or more operation invocations in a beginrescue block:
child = ...        # Get child proxy...

begin
    child.askToCleanUp()
rescue Tantrum => t
    puts "The child says: #{t.reason}"
end
Typically, you will catch only a few exceptions of specific interest around an operation invocation; other exceptions, such as unexpected run-time errors, will usually be handled by exception handlers higher in the hierarchy. For example:
def run()
    child = ...        # Get child proxy...
    begin
        child.askToCleanUp()
    rescue Tantrum => t
        puts "The child says: #{t.reason}"
        child.scold()  # Recover from error...
    end
    child.praise()     # Give positive feedback...
end

begin
    # ...
    run()
    # ...
rescue Ice::Exception => ex
    print ex.backtrace.join("\n")
end
This code handles a specific exception of local interest at the point of call and deals with other exceptions generically. (This is also the strategy we used for our first simple application in Chapter 3.)
Table of Contents Previous Next
Logo