Table of Contents Previous Next
Logo
Client-Side Slice-to-Ruby Mapping : 26.9 Mapping for Exceptions
Copyright © 2003-2009 ZeroC, Inc.

26.9 Mapping for Exceptions

The mapping for exceptions is based on the inheritance hierarchy shown in Figure 26.1
Figure 26.1. Inheritance structure for Ice exceptions.
The ancestor of all exceptions is StandardError, from which Ice::Exception is derived. Ice::LocalException and Ice::UserException are derived from Ice::Exception and form the base for all run-time and user exceptions.
Here is a fragment of the Slice definition for our world time server from Section 4.10.5 on page 125 once more:
exception GenericError {
    string reason;
};
exception BadTimeVal extends GenericError {};
exception BadZoneName extends GenericError {};
These exception definitions map to the abbreviated Ruby class definitions shown below:
class GenericError < Ice::UserException
    def initialize(reason='')

    def to_s

    def inspect

    attr_accessor :reason
end

class BadTimeVal < GenericError
    def initialize(reason='')

    def to_s

    def inspect
end

class BadZoneName < GenericError
    def initialize(reason='')

    def to_s

    def inspect
end
Each Slice exception is mapped to a Ruby class with the same name. The inheritance structure of the Slice exceptions is preserved for the generated classes, so BadTimeVal and BadZoneName inherit from GenericError.
Each exception member corresponds to an instance variable of the instance, which the constructor initializes to a default value appropriate for its type. Accessors are provided to read and write the data members.
Although BadTimeVal and BadZoneName do not declare data members, their constructors still accept a value for the inherited data member reason in order to pass it to the constructor of the base exception GenericError.
Each exception also defines the standard methods to_s and inspect to return the name of the exception and a stringified representation of the exception and its members, respectively.
All user exceptions are derived from the base class Ice::UserException. This allows you to catch all user exceptions generically by installing a handler for Ice::UserException. Similarly, you can catch all Ice run-time exceptions with a handler for Ice::LocalException, and you can catch all Ice exceptions with a handler for Ice::Exception.
Table of Contents Previous Next
Logo