Table of Contents Previous Next
Logo
Client-Side Slice-to-Java Mapping : 10.9 Mapping for Exceptions
Copyright © 2003-2008 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 115 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.15.4 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.
Exceptions have a default constructor as well as a second constructor that accepts one argument for each exception member. This constructor allows you to instantiate and initialize an exception in a single statement, instead of having to first instantiate the exception and then assign to its members. For derived exceptions, the constructor accepts one argument for each base exception member, plus one argument for each derived exception member, in base-to-derived order.
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.UserExceptions 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.
Table of Contents Previous Next
Logo