Table of Contents Previous Next
Logo
Client-Side Slice-to-Java Mapping : 10.9 Mapping for Exceptions
Copyright © 2003-2010 ZeroC, Inc.

10.9 Mapping for Exceptions

Here is a fragment of the Slice definition for our world time server from Section 4.10.5 on page 120 once more:
exception GenericError {
    string reason;
};
exception BadTimeVal extends GenericError {};
exception BadZoneName extends GenericError {};
These exception definitions map as follows:
public class GenericError extends Ice.UserException {
    public String reason;

    public GenericError() {}

    public GenericError(String reason)
    {
        this.reason = reason;
    }

    public String ice_name() {
        return "GenericError";
    }
}

public class BadTimeVal extends GenericError {
    public BadTimeVal() {}

    public BadTimeVal(String reason)
    {
        super(reason);
    }

    public String ice_name() {
        return "BadTimeVal";
    }
}

public class BadZoneName extends GenericError {
    public BadZoneName() {}

    public BadZoneName(String reason)
    {
        super(reason);
    }

    public String ice_name() {
        return "BadZoneName";
    }
}
Each Slice exception is mapped to a Java class with the same name. For each data member, the corresponding class contains a public data member. (Obviously, because BadTimeVal and BadZoneName do not have members, the generated classes for these exceptions also do not have members.) Refer to Section 10.16.3 for additional information on data members.
The inheritance structure of the Slice exceptions is preserved for the generated classes, so BadTimeVal and BadZoneName inherit from GenericError.
Each exception also defines the ice_name member function, which returns the name of the exception.
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. Ice.UserException, in turn, derives from java.lang.Exception.
Ice.UserException implements a clone method that is inherited by its derived exceptions, so you can make memberwise shallow copies of exceptions.
Note that the generated exception classes contain other member functions that are not shown. However, those member functions are internal to the Java mapping and are not meant to be called by application code.

Constructors

Exceptions have a default constructor that default-constructs each data member. This means members of primitive type are initialized to the equivalent of zero, and members of reference type are initialized to null. Note that applications must always explicitly initialize members of structure and enumerated types because the Ice run time does not accept null as a legal value for these types.
If you wish to ensure that data members of primitive and enumerated types are initialized to specific values, you can declare default values in your Slice defini­tion (see Section 4.10.2). The default constructor initializes each of these data members to its declared value.
Exceptions also have a second constructor that has one parameter for each data member. This allows you to construct and initialize a class instance in a single statement (instead of first having to construct the instance and then assigning to its members). For derived exceptions, this constructor accepts one argument for each base exception member, plus one argument for each derived exception member, in base-to-derived order.

Table of Contents Previous Next
Logo